vuex

Vuex store is undefined in vue-router

有些话、适合烂在心里 提交于 2020-01-23 03:44:05
问题 I have a router and a global beforeEach hook to validate auth. import store from "@/store/store"; const router = new Router({ // routes... }); router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requiresAuth)) { if (!store.getters.getAccessToken) { //undefined store next("/access/login"); } } else { next(); } }); export default router; And in my store/store.js file, I have an action that makes a request to validate user/password, and then tries to redirect to /

ES6 modules 详解

血红的双手。 提交于 2020-01-23 00:32:00
概述 历史上,JavaScript 一直没有模块(module)体系,无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来。其他语言都有这项功能,比如 Ruby 的 require 、Python 的 import ,甚至就连 CSS 都有 @import ,但是 JavaScript 任何这方面的支持都没有,这对开发大型的、复杂的项目形成了巨大障碍。 在 ES6 之前,社区制定了一些模块加载方案,最主要的有 CommonJS 和 AMD 两种。前者用于服务器,后者用于浏览器。ES6 在语言标准的层面上,实现了模块功能,而且实现得相当简单,完全可以取代 CommonJS 和 AMD 规范,成为浏览器和服务器通用的模块解决方案。 ES6 模块的设计思想,是尽量的静态化,使得编译时就能确定模块的依赖关系,以及输入和输出的变量。CommonJS 和 AMD 模块,都只能在运行时确定这些东西。比如,CommonJS 模块就是对象,输入时必须查找对象属性。 下面介绍一下es6的简单使用。 1. 引入模块 首先在根目录的src文件夹下创建一个test.js和index.js,在test.js中我们随便干点什么简单的事情即可。 // src/test.js console.log('first module'); 在index.js中通过import引入test.js

Vue.js[vuex] how to dispatch from a mutation?

流过昼夜 提交于 2020-01-22 17:09:49
问题 I have a list of filters I want to apply to a json object. My mutations look like this: const mutations = { setStars(state, payload) { state.stars = payload; this.dispatch('filter'); }, setReviews(state, payload) { state.reviews = payload; this.dispatch('filter'); } }; Because of how filters work I need to re-apply them all again since I can't simply keep downfiltering a list because this gets me into trouble when a user de-selects a filter option. So when a mutation is being made to a stars

Vue.js[vuex] how to dispatch from a mutation?

穿精又带淫゛_ 提交于 2020-01-22 17:09:11
问题 I have a list of filters I want to apply to a json object. My mutations look like this: const mutations = { setStars(state, payload) { state.stars = payload; this.dispatch('filter'); }, setReviews(state, payload) { state.reviews = payload; this.dispatch('filter'); } }; Because of how filters work I need to re-apply them all again since I can't simply keep downfiltering a list because this gets me into trouble when a user de-selects a filter option. So when a mutation is being made to a stars

Auto SignIn with Vuex and Vuex-persistedstate

一曲冷凌霜 提交于 2020-01-22 17:07:05
问题 I would like to auto-sign-in user when the page has been refreshed. I've read that I should use vuex-persistedstate to persist the token in localstorage . Here's my vuex store: store: { user: null }, actions: { autoSignIn ({commit}, payload) { commit('setUser', { id: payload.token }) } }, mutations: { setUser (state, payload) { state.user = payload; } }, plugins: [ createPersistedState({ getState: (key) => localStorage.getItem(key), setState: (key, state) => localStorage.setItem('user_token',

Vuex rendering data that is fetched from REST API

女生的网名这么多〃 提交于 2020-01-22 04:48:10
问题 For such component <template> <div> <router-link :to="{name:'section', params: { sectionId: firstSectionId }}">Start</router-link> </div> </template> <script lang="ts"> import { mapActions } from "vuex" export default { mounted() { this.getSectionId() }, computed: { firstSectionId() { return this.$store.state.firstSectionId } }, methods: mapActions(["getSectionId"]) } </script> Store: const store: any = new Vuex.Store({ state: { firstSectionId: null }, // actions, // mutations }) I have a web

displaying items from firebase realtime database vue store

牧云@^-^@ 提交于 2020-01-22 02:48:06
问题 i have a state state: { movies:null, items:'' }, saves data to the array then it is sent to the database i have a getter getMovies: state => { return state.movies } i want to display all the movies in the database <div v-for="item in this.$store.getters.getMovies" :key="item.id"> {{ item.name }} </div> this does not display the data 来源: https://stackoverflow.com/questions/59740866/displaying-items-from-firebase-realtime-database-vue-store

vuex笔记

早过忘川 提交于 2020-01-21 18:52:26
State 定义State const store = new Vuex.Store({ state: { todos: [ { id: 1, text: '...', done: true }, { id: 2, text: '...', done: false } ] } }) State获取 // 实例获取 store.state.count // 组件中获取 this.$store.state.count mapState辅助函数 import { mapState } from 'vuex' export default { computed: mapState({ // 箭头函数可使代码更简练 count: state => state.count, // 传字符串参数 'count' 等同于 `state => state.count` countAlias: 'count', // 为了能够使用 `this` 获取局部状态,必须使用常规函数 countPlusLocalState (state) { return state.count + this.localCount } }) } 使用扩展运算符 export default { computed: { localComputed () {}, ...mapState({ // 使用扩展运算符进行state混入

组件之间的数据传递

时光毁灭记忆、已成空白 提交于 2020-01-20 16:43:42
父子组件之间数据传递,不相关组件之间数据传递 😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍😍 一、父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据。 import HelloWorld from './components/HelloWorld.vue' 显然目前的app.vue是Hellow的父组件 通过prop从父组件获取值 //子组件helloworld.vue,通过props接收父组件传来的head值 < template > < div > {{text}} + {{head}} </ div > </ template > < script > export default { data ( ) { return { text : '父组件的内容是' } } , props : [ 'head' ] } </ script > //父组件app.vue < template > < div id = " app " > < img alt = " Vue logo " src = " ./assets/logo.png " > < HelloWorld msg = " Welcome to Your Vue.js App " :head = " headMsg " /> //通过v-bind绑定可以动态更改数据 hellow world </

Vuex的引入及其仓库store的配置

穿精又带淫゛_ 提交于 2020-01-17 19:23:42
以下是Vuex全局状态管理,数据共享具体操作: 官网:https://vuex.vuejs.org/zh/installation.html <!-- 以下是调用(获取/设置)共享数据的页面:01.vue --> <template> <div> <h3> Vuex全局状态管理,数据共享: 1,可以查看vuex官网:https://vuex.vuejs.org/zh/installation.html 2,引入vuex有两种方式: a,直接下载保存vuex.js b,(如果使用cnpm,所以下面也要使用cnpm)在项目名称下一级安装vuex: 安装: cnpm install vuex --save 卸载: cnpm uninstall vuex 3,所有插件的使用都是在src/main.js中进行导入的。包括:插件vue.js,vuex.js,App,router等。 打开src/main.js 导入如: import store from './store',并且需要在main.js中的 new Vue实例中添加:store。如下: new Vue({ el: '#app', router, store,//引用vuex仓库:store components: { App }, template: '<App/>' }) 4, 在src目录下创建stroe文件夹