vuex example02

5 篇文章 0 订阅

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> x</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.22/dist/vue.js"></script> 
<link href="https://cdn.jsdelivr.net/npm/animate.css@3.5.1" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.2.3/velocity.min.js"></script>
<!--
<script   src="C:\Users\SDSC-XAD\node_modules\vuex\dist\vuex.js" />
<script   src="https://unpkg.com/vuex@2.3.0" ></script>
-->
<script   src="https://unpkg.com/vuex@2.3.0" ></script>

 <!-- 
 参考自  https://www.cnblogs.com/first-time/p/6815036.html
 -->

</head>
<body>
    <h1>vuex</h1>
     <h2>  使用简单例子</h2>
    <hr>
  
    
    <div id="example-2">
  <button @click="show = !show">Toggle show</button>
  <transition name="bounce" :duration="{ enter: 2000, leave: 1000 }">
    <p v-if="show">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris facilisis enim libero, at lacinia diam fermentum id. Pellentesque habitant morbi tristique senectus et netus.</p>
  </transition>
  
  <abc></abc>
</div>
     
    <script type="text/javascript">
    
     //import Vue from'Vuex' 
     //    Vue.use(Vuex);
 
 
 var myStore =  new Vuex.Store({
        state:{
            //存放组件之间共享的数据
            name:"www",
            age:28,
            nums:0
        },
         mutations:{
             //显式的更改state里的数据
             change:function(state,a){
                //  state.num++;
               console.log(state.nums += a); 
               
             },
             changeAsync :function(state,a){
                //  state.num++;
               console.log(state.nums += a); 
               
             }
         },
         getters:{
             getAge:function(state){
             var s="too young"
                 if(state.age<=28){
                  return s;
                 }
                // return state.age;
             }
         },
         actions:{
             //设置延时
             add:function(context,value){
                 setTimeout(function(){
           //提交事件
                    context.commit('changeAsync',value);
                 },1000)
                 
             }
         }
    });
      
 
   
  
  Vue.component('abc',{
 
        template:`
                <div>
                    <p @click='changeNum'>姓名:{{abcname}} 年龄:{{age}} 次数:{{nums}}</p>
                    <button @click='changeNumAnsyc'>change</button>
                </div>`,         
         
         mounted:function(){
            console.log(this)
        },
        computed: {
            abcname:function(){
                //return this.$myStore.state.name
                return this.$store.state.name
            },
            age:function(){
                 //return this.$store.state.age
                 //使用getters
                 return this.$store.getters.getAge 
            },
            nums:function(){
                 //return this.$store.state.age
                 //使用getters
                 return this.$store.state.nums 
            }
        },
         methods: {
            changeNum: function(){
                //在组件里提交
                // this.num++;
                this.$store.commit('change',1)
            },
            //在组件里派发事件 当点击按钮时,changeNumAnsyc触发-->actions里的add函数被触发-->mutations里的changeAsync函数触发


            changeNumAnsyc:function(){
                this.$store.dispatch('add', 5);
            }
        },
    })
     
   
        new Vue({
          el: '#example-2',
          data: {
            show: true             
          },
         store:myStore,
        })
        
          
     
    </script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vuex is a state management pattern and library for Vue.js applications. It serves as a centralized store for managing the state of an application, making it easier to track and manipulate data across components. Vuex follows the Flux architecture, where the state of an application is stored in a single centralized location called the store. Vuex organizes the state into modules, which allows for a modular structure of the store. Each module can have its own state, mutations, actions, and getters. This helps in separating concerns and keeping the codebase more maintainable and scalable. By using Vuex modules, you can break down the application state into smaller, manageable pieces. Each module can have its own set of mutations to modify the state, actions to perform asynchronous tasks, and getters to retrieve computed values from the state. To use Vuex modules in a Vue.js application, you need to install Vuex by running `npm install vuex` or `yarn add vuex` in your project's directory. Then, create a store instance by importing Vuex and defining modules with their respective state, mutations, actions, and getters. Finally, mount the store instance to the Vue application to make it available across components. Here's a basic example of how to define a Vuex module: ```javascript // store.js import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const moduleA = { state: { // module A state }, mutations: { // module A mutations }, actions: { // module A actions }, getters: { // module A getters } }; const moduleB = { state: { // module B state }, mutations: { // module B mutations }, actions: { // module B actions }, getters: { // module B getters } }; export default new Vuex.Store({ modules: { moduleA, moduleB } }); ``` This is just a basic example, and there are more advanced features and concepts in Vuex. I hope this gives you an overview of Vuex modules and how they can be used in a Vue.js application. Let me know if you have any more questions!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值