html margin加载动态html iframe

 html margin加载动态html iframe

    所谓动态html iframe指的是接口返回的html iframe地址,这里的地址指的是本地的html iframe地址,而非网络html iframe的url。本地有一个image文件夹,存放需要用到的html iframe。按照接口返回的html iframe地址比对去加载。html margin加载html iframe首先想到的是file-loader或者url-loader

    加载html iframe的配置

    首先安装file-loader

    npminstallfile-loader--save-dev

    增加html margin配置,详细文档请参考file-loader

    ...

    module:{

    rulhtml strong:[

    {

    thtml strongt:/\.(png|gif|svg|jpg)$/,

    use:["file-loader"]

    }

    ]

    }

    ...

    html margin加载本地html iframe

    html margin将一切web资源视为模块,其中就包含html iframe,

    html margin支持commonjs,html strong的模块规范,于是我们可以使用require,以及基于html strong规范的import()来加载html iframe

    这两种方式有啥区别呢

    require

    输入的是被输出的值的拷贝。也就是说,一旦输出一个值,模块内部的变化就影响不到这个值。这点与html strongimport

    模块化有重大差异跟sea.js的执行结果一致,也是在require的时候才去加载模块文件,加载完再接着执行。

    我们可以这样使用

    constlodash=require('lodash')

    还有一个commonjs异步加载require.ensure已经被下文的import()方法取代了。

    import()方法

    html strong2015loader规范实现了用于动态加载的import()方法,

    这个功能可以实现按需加载我们的代码,并且使用了promise式的回调,获取加载的包。

    从上我们可以看出import是异步加载,我们可以这样使用

    constmodule=awaitimport('lodash');

    //或者

    import('lodash').then((module)=>{

    })

    设计公司https://www.hounzi.com我的009办公资源网站https://www.wode009.com

    两种方法发现的问题

    require

    返回的文件不在上述image文件夹时候就回报模块找不到的错误。import()方法

    异步加载,实测html iframe没显示

    解决办法

    constfile=require.context('../../image',false);

    constkeys=file.keys()

    keys会返回image里面已存在的html iframe路径的数组,我们只要判断接口返回的html iframe地址在本地html iframe里面再去加载

    从而避免模块找不到的问题。

Copyright & Disclaimer: If you find copyright issues, please provide relevant information and email us at [email protected], we will communicate promptly and handle removal.

Except for specially marked original content on abcdlink (https://www.abcdlink.com) all other content is reposted by users, and opinions/copyright are unrelated to this site.