vuex

Vue 项目 login 模块

荒凉一梦 提交于 2019-12-06 06:56:30
登录模块 需要做路由的守卫,有些模块是需要被保护的,必须登录才能访问。 常见手法是给路由添加 meta:{ auth:true } 来做标识,表示需要做认证。 // 路由守卫 router.beforeEach((to, from, next) => { // 判断要进入的路由是否需要认证 if(to.meta.auth) { // 通过token令牌机制判断是否已经登录 const token = localStorage.getItem('token'); if (token) { next(); // 如果登录则放行,进入路由 } else { // 跳转,并携带重定向地址 next({ path: '/login', query: { redirect: to.path } }); } } else { // 不需要验证的模块,直接放行 next(); } }); 在 Vuex 中存储登录状态 isLogin import Vue from 'vue' import Vuex from 'vuex' import user from './service/user' Vue.use(Vuex); export default new Vuex.Store({ state: { isLogin: localStorage.getItem('token') ? true :

Reload Data of vue-tables-2 (Vuex)

风格不统一 提交于 2019-12-06 04:26:51
问题 Module: https://github.com/matfish2/vue-tables-2 I'm creating a CRUD app. How can I reload the data fetched via Ajax call in vue-tables-2? I wanted to reload the table after an update statement is executed somewhere in my app. Vue-tables is using vuex in my setup. <v-server-table name="UserAdmin" url="admin/master/?format=json" :columns="columns" :options="options"> </v-server-table> EDIT: Added Javascript code of the table for data properties. export default { data() { return { columns: ['ID

Data() VS asyncData() in Nuxt & vue

本小妞迷上赌 提交于 2019-12-06 04:04:40
问题 both data() and async data() gives the same result (and it is obvious that the results from asyncData() override the results from data() ) and both results in HTML code in the source code (i.e the code rendered in the server side) also both can be used to " await " the data to be fetched (ex: using axios) so, what is the difference between them? <template> <div> <div>test: {{test}}</div> <div>test2: {{test2}}</div> <div>test2: {{test3}}</div> <div>test2: {{test4}}</div> </div> </template>

vuex封装购物车函数

谁说我不能喝 提交于 2019-12-06 02:56:43
js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state:{ shopCart:[], allChecked:false }, mutations:{ setShopCart(state,obj){ state.shopCart.push(obj); }, calcUp(state,index){ state.shopCart[index].Num == state.shopCart[index].total?state.shopCart[index].total : state.shopCart[index].Num++ }, calcDown(state,index){ state.shopCart[index].Num == 0?0 : state.shopCart[index].Num-- }, ShopVal(state,index){ var shopVal = state.shopCart[index].Num; shopVal = isNaN(shopVal)? 0 : Math.ceil(shopVal); shopVal = shopVal<0?0:shopVal; shopVal = shopVal>state

Vue开发之底部导航栏

大憨熊 提交于 2019-12-06 02:40:17
一、导航切换 封装一个公用组件Tabbar,在需要导航页的页面引入组件即可。代码如下: <template> <div class="tabbar"> <!-- 占位容器 --> <div class="placegolder-container"></div> <!-- 底部导航栏 --> <div class="bottom-tabs"> <div class="tabs-item" v-for="(item, index) in tabsList" :key="index" @click="tabsChange(index)" > <img class="tab-icon" :src="tabIndex==index?item.src:item.src1"> <p class="tab-text" :class="tabIndex==index?'active':''">{{item.text}}</p> </div> </div> </div> </template> <script> export default { name: "tabbar", components: {}, created() { this.tabIndex = localStorage.getItem("tabIndex"); console.log(this.tabIndex,

Vue listen for Vuex commit?

帅比萌擦擦* 提交于 2019-12-06 01:46:29
Is there a way to listen to a Vuex commit without watching any of the properties that are changed with the commit? Just simply finding out if a commit has happened? I have a Filter component that I want to put into a NPM package but I already have a use case where I would want to set a cookie storing the filter preferences whenever a filter is selected. Obviously it is not the responsibility of the filter component to set cookies etc. and this is something that should be optional. I guess one way would be to use a global event bus but this would mean a user which uses my package would have to

Vuex $store properties not reactive when using computed property

被刻印的时光 ゝ 提交于 2019-12-05 22:31:11
问题 I have a Vuex store, which I'm injecting into my instance: import store from '../store'; const mainNav = new Vue({ el: '#main-nav', store, components: { NavComponent } }); And I'm creating a computed property from that store in the component: computed: { isWide() { return this.$store.state.nav.type === 'wide'; } } This does create the this.isWide property for the template on component initialization, but when the store value is updated, the component doesn't register this - the old value is

Vue.js disable component during ajax request

倾然丶 夕夏残阳落幕 提交于 2019-12-05 21:40:16
I'm looking for a simple solution for the following problem: I have a Vue component button with which I can make an ajax request. I would like to disable this button while the request is pending (to prevent multiple requests). Sounds like you want your action to set (commit) a flag when it starts and then clear it when it ends. Try something like this in Vuex... state: { loading: false }, mutations: { isLoading (state) { state.loading = true }, doneLoading (state) { state.loading = false } }, actions: { doAjaxRequest ({ commit }) { commit('isLoading') return doSomeAjaxRequest().then(res => { /

Vuex 刷新后数据丢失问题 Typescript

匆匆过客 提交于 2019-12-05 20:34:19
问题描述:Vuex保存的数据在页面刷新后会全部丢失清除 问题解决方案:使用sessionstorage进行保存,在页面刷新时保存至sessionStorage,页面在加载时再进行填充 (另有vuex-persistedstate 插件可以解决这个问题,本人是使用typescript,使用‘vuex-module-decorators’注册vuex各module,不知如何使用该插件,就没有尝试过,想试试的可以去搜索,很多这方面文章) 在App.vue 中 import { UserModule } from '@/store/modules/user' export default { created() { //在页面加载时读取sessionStorage里的状态信息 if (sessionStorage.getItem('store')) { this.$store.replaceState( Object.assign( {}, this.$store.state, JSON.parse(sessionStorage.getItem('store')) ) ) } //在页面刷新时将vuex里的信息保存到sessionStorage里 window.addEventListener('beforeunload', () => { console.log('setstore')

vuex的使用

安稳与你 提交于 2019-12-05 18:10:39
vuex是配合vue一块儿使用的一个状态管理工具。 我通常使用它来保存一些全局的数据,例如用户登录信息,用户身份信息,总之一些在很多页面都会使用到的信息,都保存在vuex里面,用的时候就不需要再去请求接口了,直接去vuex里面拿就可以了。 先放 官网地址 安装 npm install vuex --save 配置 我实在vue-cli环境中使用vuex的,所以这里就以这个环境作为项目文件结构来写配置了。 先在src/assets文件夹中新建一个vuex/store.js文件,建好之后在文件中写如下代码: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { name: 'xiaoming', count: 1 } }) export default store 在main.js中引入store.js: import store from '@/assets/vuex/store'; 在全局构造器中注册一下:(这里千万别忘了) new Vue({ el: '#app', router, components: { App }, template: '<App/>', store: store //在根实例下面注册store,相当于全局注册