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>
相关推荐
独泪了无痕5 小时前
Vue调试神器:Vue DevTools使用指南
vue.js·前端工程化
优秀稳妥的JiaJi9 小时前
基于腾讯地图实现电子围栏绘制与校验
前端·vue.js·前端框架
Lee川10 小时前
深度解构JavaScript:作用域链与闭包的内存全景图
javascript·面试
好雨知时节t10 小时前
Pinia中defineStore的使用方法
vue.js
_Eleven11 小时前
Pinia vs Vuex 深度解析与完整实战指南
前端·javascript·vue.js
技术狂小子11 小时前
# 一个 Binder 通信中的多线程同步问题
javascript·vue.js
进击的尘埃11 小时前
Service Worker + stale-while-revalidate:让页面"假装"秒开的那些事
javascript
秋水无痕12 小时前
从零搭建个人博客系统:Spring Boot 多模块实践详解
前端·javascript·后端
进击的尘埃12 小时前
基于 Claude Streaming API 的多轮对话组件设计:状态机与流式渲染那些事
javascript
juejin_cn12 小时前
[转][译] 从零开始构建 OpenClaw — 第六部分(持久化记忆)
javascript