Vuejs在webpack模板中延迟加载路由(Vuejs lazy loading routes in webpack template)

编程入门 行业动态 更新时间:2024-10-22 16:48:13
Vuejs在webpack模板中延迟加载路由(Vuejs lazy loading routes in webpack template)

我用vue-cli工具和webpack模板创建了vuejs项目。

vue init webpack my-project

我不知道如何在模板路径上实现延迟加载

目前我有两个路由在router/index.js

import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import Test from '@/components/Test' Vue.use(Router) export default new Router({ mode: 'history', routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld }, { path: '/test', name: 'test', component: Test } ] })

并在main.js导入

import Vue from 'vue' import App from './App' import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' })

但它不包含延迟加载实现我该怎么做?

I have created vuejs project with vue-cli tools and webpack template.

vue init webpack my-project

I don't know how to implement lazy loading on routes with templates

currently i have two routes in router/index.js

import Vue from 'vue' import Router from 'vue-router' import HelloWorld from '@/components/HelloWorld' import Test from '@/components/Test' Vue.use(Router) export default new Router({ mode: 'history', routes: [ { path: '/', name: 'HelloWorld', component: HelloWorld }, { path: '/test', name: 'test', component: Test } ] })

And its imported in main.js

import Vue from 'vue' import App from './App' import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, components: { App }, template: '<App/>' })

But it doesn't contain the lazy loading implementation how can i do it??

最满意答案

而不是使用

import Test from '@/components/Test'

用于

const Test = () => import('@/components/Test');

延迟加载Documentataion

instead of using

import Test from '@/components/Test'

use as

const Test = () => import('@/components/Test');

lazy loading Documentataion

更多推荐

本文发布于:2023-08-07 02:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   加载   模板   webpack   Vuejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!