这次给大家带来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
//点击按钮后,show为真,先获取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 router让子路由全部分离为独立组件
vue.js前后端数据交互步骤详解
以上就是vue+webpack异步加载方法总结的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2767308.html