Html利用Vue动态加载单文件页面【httpVueLoader】

文章目录

1、 首先页面引入vue、http-vue-loader

html 复制代码
  <script src="./assets/js/vue.min.js"></script>
  <script src="https://unpkg.com/http-vue-loader"></script>

2、 利用httpVueLoader加载指定页面

html 复制代码
<div id="app">
<addressComponent ></addressComponent>
 </div>
<script>
  var addressComponent = httpVueLoader('./file.vue');
  var app = new Vue({
    el: '#app',
    components: {
      addressComponent,
    },
  })
</script>

3、利用httpVueLoader加载的文件【file.vue】

3.1 js module.export导出

注意:httpVueLoader加载的单文件导出方式不同:module.exports = {},而不是export default {}

html 复制代码
<template>
    <div>{{title}}</div>
</template>
<script>
module.exports = {
    data(){
        return {
            title: "动态页面"
        }
    },
}
</script>

3.2 通过 import 加载外部JS

javascript 复制代码
<script>
module.exports = {
    data(){
        return {
            title: "动态页面"
        }
    },
    async mounted(){
        const fn = await import('./js/file-fn.js');
        fn.fileFn(); //123
    }
}
</script>
javascript 复制代码
// file-fn.js
export function fileFn() {
	console.log(123);
}

4、httpVueLoader:组件的全局注册和局部注册

4.1、全局注册

html 复制代码
<template>
    <div class="home">
        <my-header></my-header>
    </div>
</template>
 
<script>
    //引入,相当于import
    const Header = httpVueLoader('./Header.vue');
    //全局注册
    Vue.component('my-header', Header);
    module.exports = {
        data() {
            return { };
        }
    };
</script>
 
<style scoped>
    .home {
        font-size: 24px;
        font-weight: bold;
    }
</style>

4.2、局部注册

html 复制代码
<template>
    <div class="home">
        <my-header></my-header>
    </div>
</template>
 
<script>
    //引入 相当于import
    const Header = httpVueLoader('./Header.vue');
    module.exports = {
        data() {
            return {
                msg: '主页内容'
            };
        },
       //局部注册
      components:{
        'my-header':Header
      }
    };
</script>
 
<style scoped>
    .home {
        font-size: 24px;
        font-weight: bold;
    }
</style>
相关推荐
雪芽蓝域zzs5 分钟前
第3节:el-select 弹窗定位、滚动穿透与挂载容器(popper-append-to-body /teleported)
vue.js·elementui
铅笔小新z1 小时前
【stm32】理解弱引用
javascript·stm32·嵌入式硬件
凤城老人2 小时前
从PyQt6到Electron:一款英语字帖生成器的跨平台重构实战与踩坑全记录
javascript·重构·electron
雪芽蓝域zzs10 小时前
第 2 节:基础扩展 - 下拉弹窗常用原生属性
vue.js·elementui
星栈13 小时前
pnpm 12 升级实测
前端·javascript
Ai-_Man14 小时前
您您这可以把Microsofat Copilot的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。AI导出鸭
javascript·人工智能·ai·小程序·电脑·copilot
Bruce_Liuxiaowei17 小时前
网页版 DeepSeek “harness desktop“ WebGL 上下文创建失败排查实录
开发语言·javascript·webgl
小凯在掘金17 小时前
为什么箭头函数不能作为构造函数?
前端·javascript
可乐鸡翅yeah_18 小时前
网页端 M3U8 播放器性能监控,怎么看播放卡顿指标
javascript·ffmpeg·音视频·safari·m3u8
可乐鸡翅yeah_18 小时前
M3U8 防盗链 URL‑Token 签名,新手开发实操避坑
开发语言·javascript·ios·音视频·safari