[Vuex] Lazy Load a Vuex Module at Runtime using TypeScript
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的更多相关文章
- [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 ...
- [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 ...
- Ionic 3 延迟加载(Lazy Load)实战(一)
本文分享并演示了在 Ionic 3 框架中如何进行模块的延迟加载(Lazy Load)开发. 在我的实战课程「快速上手Ionic3 多平台开发企业级问答社区」中,因为开发的仿知乎 App 模块间的加载 ...
- Ninject Lazy Load问题
参考: http://stackoverflow.com/questions/2538132/lazy-loading-with-ninject 方案一: public class Module : ...
- Lazy Load, 延迟加载图片的 jQuery 插件.
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- jQuery延迟加载插件(Lazy Load)详解
最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...
- 延迟加载图片的 jQuery 插件:Lazy Load
网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...
- Lazy Load 图片延迟加载(转)
jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...
- jQuery Lazy Load 图片延迟加载
基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. 版本: jQuery v1.4.4+ jQuery ...
随机推荐
- java06作业归档
动手动脑 阅读QiPan.java示例程序了解如何利用二维数组和循环语句绘制五子棋盘. package 归档作业6; import java.io.*; public class QIPAN { // ...
- Java常用API——时间类
前言:Java.util.*工具包中,包含了集合框架,旧集合类,事件模型,日期和时间设施,国际化和其他使用程序类 (字符串.随机数生成器和位数组) 一.日期类Date 1.概述 Date是一个薄包装类 ...
- hexo添加404公益界面
http://hellolb.top/2018/08/16/hexo添加404公益界面/ hexo个人博客添加404公益界面,这里我使用的腾讯404公益界面 我的博客主题是hexo+yilia,其实所 ...
- NIO缓冲区基本操作:rewind(),clear(),flip()
rewind() rewind()方法将position置0,清除mark,它的作用在于为提取Buffer的有效数据做准备. ByteBuffer byteBuf = ByteBuffer.alloc ...
- Android-自定义View前传-View的三大流程-Layout
Android-自定义View前传-View的三大流程-Layout 参考 <Android开发艺术探索> https://github.com/hongyangAndroid/FlowL ...
- Xamarin Essentials教程实现数据的传输功能实例
Xamarin Essentials教程实现数据的传输功能实例 [示例1-1]以下将实现数据的传输功能.代码如下: public async Task ShareUri(string text, st ...
- 杭电2000----ASCII码排序
#include<stdio.h> int main() { ]; int i,j,t; while(gets(a)!=NULL) { ; i<; ++i) ; j<-i; + ...
- [JOISC2014]たのしい家庭菜園
[JOISC2014]たのしい家庭菜園 题目大意: 给定一个长度为\(n(n\le3\times10^5)\)的序列\(A(A_i\le10^9)\).只能交换相邻两个数,问最少需要几步可以将它变成一 ...
- Victoria的舞会2——图的连通性及连通分量
[Vijos1022]]Victoria的舞会2 Description Victoria是一位颇有成就的艺术家,他因油画作品<我爱北京天安门>闻名于世界.现在,他为了报答帮助他的同行们, ...
- linux VPS服务器的一些配置
SSH密钥登录让Linux VPS/服务器更安全 2011年01月10日 上午 | 作者:VPSer 随着PHP越来越流行,Linux VPS/服务器的使用也越来越多,Linux的安全问题也需要日渐加 ...