Sometimes we need to create modules at runtime, for example depending on a condition. We could even want to lazy load that module by using Webpack’s code splitting feature.

For example, in current appliation, we are loading login and todos modules at the same time, now what we want is lazy load login module:

import Vue from 'vue';
import Vuex from 'vuex'; import {todos} from './todos';
import {login} from './login'; Vue.use(Vuex); export default new Vuex.Store({
modules: {
todos,
login,
}
});

Go to the main.ts file, set login module to be lazy load:

import './hooks';

import Vue from 'vue';
import App from './App.vue';
import router from '@/router';
import store from '@/store/index';
import '@/registerServiceWorker';
Vue.config.productionTip = false; const load = true;
if (load) {
import('./store/login').then(({login}) => {
store.registerModule('login', login);
});
} new Vue({
router,
store,
render: (h) => h(App),
}).$mount('#app');

We can use dynamic import syntax to lazy load login store.

To support the syntax, we need to change "compilerOptions.modules='esnext'".

To code safty. in login component, we add v-if to check the login module is loaded or not:

<template>
<section v-if="login">
<button v-if="!login.isLoggedIn" @click="loginMutation">Login</button>
<p v-else>Hello {{login.user}}</p>
</section>
</template> <script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import {State, Mutation} from 'vuex-class';
import {LoginState} from '../types'; @Component()
export default class Login extends Vue{
@State login: LoginState;
@Mutation('login') loginMutation;
}
</script>

[Vuex] Lazy Load a Vuex Module at Runtime using TypeScript的更多相关文章

  1. [Angular] Lazy Load CSS at runtime with the Angular CLI

    Ever had the need for multiple "app themes", or even to completely dynamically load CSS ba ...

  2. [Angular2 Router] Lazy Load Angular 2 Modules with the Router

    Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start ...

  3. Ionic 3 延迟加载(Lazy Load)实战(一)

    本文分享并演示了在 Ionic 3 框架中如何进行模块的延迟加载(Lazy Load)开发. 在我的实战课程「快速上手Ionic3 多平台开发企业级问答社区」中,因为开发的仿知乎 App 模块间的加载 ...

  4. Ninject Lazy Load问题

    参考: http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject  方案一: public class Module : ...

  5. Lazy Load, 延迟加载图片的 jQuery 插件.

    Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...

  6. jQuery延迟加载插件(Lazy Load)详解

    最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...

  7. 延迟加载图片的 jQuery 插件:Lazy Load

    网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...

  8. Lazy Load 图片延迟加载(转)

    jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...

  9. jQuery Lazy Load 图片延迟加载

    基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...

随机推荐

  1. 清除DNS缓存(解决能上QQ但是无法上网页问题)

    ipconfig/displaydnsipconfig/flushdns

  2. CentOS 7.2配置Apache服务httpd小伙伴们可以参考一下

    这篇文章主要为大家详细介绍了CentOS 7.2配置Apache服务 httpd上篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 一.Perl + mod_perl 安装mod_perl使Per ...

  3. Kafka Manager

    1.kafka Manager 1.上传压缩包kafka-manager-1.3.3.15.zip到集群 2.解压到/opt/module 3.修改配置文件conf/application.conf ...

  4. 基于C语言的Socket网络编程搭建简易的Web服务器(socket实现的内部原理)

    首先编写我们服务器上需要的c文件WebServer.c 涉及到的函数API: int copy(FILE *read_f, FILE * write_f) ----- 文件内容复制的方法 int Do ...

  5. responseHandler

    resonsehandler 接受服务端传过来的数据,然后在这个函数里处理好要显示的数据在return个table显示 <!DOCTYPE html> <html lang=&quo ...

  6. POJ1062昂贵的聘礼(经典) 枚举区间 +【Dijkstra】

    <题目链接>                   昂贵的聘礼 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用1000 ...

  7. Python常用模块--re

    Python内部的re--传闻中的正则模块,是无数初学者心中的噩梦,几乎到了谈正则色变的地步. 1.正则是干什么的 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常 ...

  8. C# 动态调用WebService 2

    using Microsoft.CSharp; using System; using System.CodeDom; using System.CodeDom.Compiler; using Sys ...

  9. 鼠标hover时图片效果

    <!DOCTYPE html><head><meta http-equiv="Content-Type" content="text/htm ...

  10. DataGrid绑定DataTable出错

    直接用DataGrid.ItemSource = DataTable.DefaultView时会出现以下错误: target element is 'TextBlock' (Name=''); tar ...