import{_ as n}from"./chunks/AdminExample.vue_vue_type_script_setup_true_lang.wPcM-fsm.js";import{o as h,c as k,j as i,a,E as t,a3 as l}from"./chunks/framework.D8G7yXPy.js";const g=JSON.parse('{"title":"RIframe","description":"","frontmatter":{},"headers":[],"relativePath":"reference/components/iframe.md","filePath":"reference/components/iframe.md"}'),p={name:"reference/components/iframe.md"},y=Object.assign(p,{setup(e){return(E,s)=>(h(),k("div",null,[s[0]||(s[0]=i("h1",{id:"riframe",tabindex:"-1"},[a("RIframe "),i("a",{class:"header-anchor",href:"#riframe","aria-label":'Permalink to "RIframe"'},"​")],-1)),s[1]||(s[1]=i("p",null,[i("code",null,"RIframe"),a(" 封装原生 iframe 和 Naive UI Spin。它适合嵌入允许被加载的页面,页面自身的 CSP、嵌入授权和同源策略仍生效。"),i("code",null,"load"),a(" 事件并不能证明业务内容加载成功。")],-1)),s[2]||(s[2]=i("h2",{id:"交互示例",tabindex:"-1"},[a("交互示例 "),i("a",{class:"header-anchor",href:"#交互示例","aria-label":'Permalink to "交互示例"'},"​")],-1)),t(n,{path:"/iframe",title:"嵌入页面和加载状态"}),s[3]||(s[3]=l(`
查看示例源码
tsx
import { RIframe } from '@/components'
import { useI18n } from '@/hooks'
import { NButton, NCard, NTag } from 'naive-ui'
import DemoPage from '../components/DemoPage'

export default defineComponent({
  name: 'IframeDemo',
  setup() {
    const { t } = useI18n('demoDetails')
    const lazyMounted = ref(false)
    const loaded = reactive({ eager: false, lazy: false })
    const source = \`\${import.meta.env.BASE_URL}examples/embed.html\`

    return () => (
      <DemoPage title={t('iframe.title')} description={t('iframe.intro')}>
        <div class="r-demo-grid">
          {(['eager', 'lazy'] as const).map((mode) => (
            <NCard key={mode} title={t(\`iframe.\${mode}\`)}>
              {{
                'header-extra': () => (
                  <NTag
                    size="small"
                    bordered={false}
                    type={loaded[mode] ? 'success' : 'default'}
                  >
                    {loaded[mode] ? t('iframe.loaded') : t('iframe.pending')}
                  </NTag>
                ),
                default: () =>
                  mode === 'eager' || lazyMounted.value ? (
                    <RIframe
                      src={source}
                      title={t(\`iframe.\${mode}\`)}
                      height={220}
                      lazy={mode === 'lazy'}
                      onSuccess={() => {
                        loaded[mode] = true
                      }}
                    />
                  ) : (
                    <div class="r-demo-stage">
                      <NButton
                        secondary
                        onClick={() => {
                          lazyMounted.value = true
                        }}
                      >
                        {t('iframe.mount')}
                      </NButton>
                    </div>
                  ),
              }}
            </NCard>
          ))}
        </div>
      </DemoPage>
    )
  },
})
tsx
import { RIframe } from '@/components'

export const renderReport = (url: string) => (
  <RIframe src={url} title="库存报表" height={460} lazy="eager" />
)
属性默认说明
src必填iframe 地址。接收用户输入时应由业务限制可嵌入来源。
width / height'100%'CSS 尺寸。百分比高度依赖父级。
frameborder0模板边框样式变量。
iframeWrapperClass / iframeClass无外层容器与原生 iframe 的类名。
title / name / allow无原生 iframe 属性。title 用于辅助技术识别。
lazytruetrue / 'lazy' 延迟加载,false / 'eager' 主动加载。
customSpinProps{}NSpin 配置,show 由组件内状态覆盖。

onSuccess(iframe, event) 和 onError(event) 支持函数或回调数组。每次 src 变化都会重新显示 Spin,在 load / error 后关闭。组件 ref 上的 iframe 和兼容别名 iframeInst 都是原生 DOM 元素(尚未挂载时为 null);Vue 已自动解包,无需再访问 .value。额外插槽交给 Spin,但默认内容始终为 iframe。

组件实现 · Props

`,5))]))}});export{g as __pageData,y as default};