最近遇到用swagger写api接口,刚开始是在index.js用script标签引入的,后来负责人说拖累首次加载速度,要我按需引入,引个锤子,就用了一次,直接组建内部引入
1.在组建内部,加载创建一个script标签
在mounted(){ //dom加载完,还没加载数据的时候
const s = document.createElement('script');
s.type = 'text/javascript';
// s.src = './../../../static/swagger-ui/swagger-ui-standalone-preset.js';
s.src = '/dist/jtzy/swagger-ui-bundle.js';
document.body.appendChild(s);
}
2.组件引入法
export default {
components: {
'dingtalk': {
render(createElement) {
return createElement(
'script',
{
attrs: {
type: 'text/javascript',
src: '/dist/jtzy/swagger-ui-bundle.js',
},
},
);
},
},
},
}
// 使用 <dingtalk></dingtalk> 在页面中调用
3.组建引入另外一种
export default {
components: {
'remote-js': {
render(createElement) {
return createElement('script', { attrs: { type: 'text/javascript', src: this.src }});
},
props: {
src: { type: String, required: true },
},
},
},
}
<remote-js src="/dist/jtzy/swagger-ui-bundle.js"></remote-js>