The simplest application of store in Vue is global storage. I use two components here to jump to each other ( First you need to install vuex: Because you need to jump, you have to install the router: Create a new store folder and create a new modules folder, Create Put our variable msg in mystate: const state = { msg: 'This is my status', } export default { state } Getters hold the key-value pairs of the variables we operate on: const getters = { msg:state => state.mystate.msg, } export default getters Index is used to configure and create import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' Vue.use(Vuex) // https://webpack.js.org/guides/dependency-management/#requirecontext const modulesFiles = require.context('./modules', true, /\.js$/) // It will automatically require all vuex modules in your module file // you do not need `import app from './modules/app'` // it will auto require all vuex modules from modules file const modules = modulesFiles.keys().reduce((modules, modulePath) => { // set './app.js' => 'app' const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules }, {}) const store = new Vuex.Store({ modules, getters, }) export default store You need to call store and router in the vue instance of import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' Vue.use(Vuex) // https://webpack.js.org/guides/dependency-management/#requirecontext const modulesFiles = require.context('./modules', true, /\.js$/) // It will automatically require all vuex modules in your module file // you do not need `import app from './modules/app'` // it will auto require all vuex modules from modules file const modules = modulesFiles.keys().reduce((modules, modulePath) => { // set './app.js' => 'app' const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules }, {}) const store = new Vuex.Store({ modules, getters, }) export default store Configure two routes in import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' Vue.use(Vuex) // https://webpack.js.org/guides/dependency-management/#requirecontext const modulesFiles = require.context('./modules', true, /\.js$/) // It will automatically require all vuex modules in your module file // you do not need `import app from './modules/app'` // it will auto require all vuex modules from modules file const modules = modulesFiles.keys().reduce((modules, modulePath) => { // set './app.js' => 'app' const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') const value = modulesFiles(modulePath) modules[moduleName] = value.default return modules }, {}) const store = new Vuex.Store({ modules, getters, }) export default store Use the route view in App.vue: <template> <div id="app"> <router-view></router-view> </div> </template> <script> export default { name: 'App' } </script> <style> #app { font-family: 'Avenir', Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } </style> Finally, there are two page components, Two-way binding and monitoring of msg, putting the new value of msg into the global variable. The method for monitoring triggering is setstate; <template> <div class="hello"> <img src="../assets/logo.png" /> <br /> <input v-model="msg"/> <h2>{{ msg }}</h2> <router-link to="/two">I want to go to the second page</router-link> </div> </template> <script> export default { name: "HelloWorld", data() { return { msg: "Welcome to new vue project", }; }, methods: { setstate(value) { sessionStorage.setItem('msg', value); }, }, watch: msg(newName, oldName) { this.setstate(newName); }, }, }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> a { color: #42b983; } </style> In the second page component <template> <div> This is the second page<h2>{{ msg }}</h2> <router-link to="/">I want to go back</router-link> </div> </template> <script> export default { data() { return { msg: "", }; }, methods: { setmsg() { this.msg = sessionStorage.getItem('msg'); }, }, created(){ this.setmsg() } }; </script> Directory structure: Demo: Initial state: To change the content of the input box: Go to the second page: SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Steps to build MHA architecture deployment in MySQL
>>: Nginx Service Quick Start Tutorial
Table of contents 1. Resources and Construction 1...
Overview This article will introduce the MVC arch...
Table of contents Preface🌟 1. API Introduction 2....
1. This is a bit complicated to understand, I hop...
Table of contents Docker image download Start mys...
When the jsp that is jumped to after the struts2 a...
Table of contents 1. Knowledge description of the...
This article mainly introduces how to use the Rea...
Table of contents Overview 1. Dependency Injectio...
1. To optimize the query, try to avoid full table...
1. What is master-slave replication? Master-slave...
Table of contents Preface 1. Reasons: 2. Solution...
radio-and-checkbox Pure CSS to achieve radio and ...
There are many types of auto-increment IDs used i...
Table of contents 1. What is copy_{to,from}_user(...