您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

有哪些方法可以使vue+webpack实现异步加载

2024/8/19 19:59:09发布40次查看
这次给大家带来有哪些方法可以使vue+webpack实现异步加载,使vue+webpack实现异步加载的注意事项有哪些,下面就是实战案例,一起来看一下。
1.第一例
const home = resolve => {   import(@/components/home/home.vue).then( module => {       resolve(module)   } }
注:(上面import的时候可以不写后缀)
export default [{   path: '/home',   name:'home',   component: home,   meta: {     requireauth: true, // 添加该属性可以判断出该页面是否需要登录显示   }, }]
2.第二例
const router = new router({   routes: [     {        path: '/home',        component: (resolve)=> {          require(['../components/home/home'], resolve) // 省去了在上面去import引入        }      }   ] })
3.第三例,这也是推荐的一种
// r就是resolve// 路由也是正常的写法 这种是官方推荐的写的 按模块划分懒加载  const home = r => require.ensure([], () => r(require('../components/home/home')), 'home'); const router = new router({   routes: [     {      path: '/home/home',      component: home,      name: 'home' ,     }   ] })
下面给大家介绍下vue+webpack实现异步组件加载的代码,具体代码如下所示:
html
<input type="button" @click="showchild" value="show"> //点击按钮后,show为真,先获取child组件,再渲染p内容  <p id="contain" v-if="show">   <child></child> </p>
js
data () {   return {     msg: 'welcome to your vue.js app',     show:false   } }, methods: {   showchild:function(){     this.show=true;   } }, components: {   'child': function(resolve) {     require(['./components/child.vue'], resolve);   } }
注意:加载异步组件的时候,组件名后边的.vue不要忽略。这个例子应该比较直观了。点击按钮之后改变了变量show的布尔值为真,由于child.vue是异步组件,所以会先ajax获取组件然后渲染。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
如何实现vue项目中vue-i18n和element-ui国际化开发
你必须要注意的vue组件使用细节
以上就是有哪些方法可以使vue+webpack实现异步加载的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product