先来了解一下官网https://www.nuxtjs.cn/guide/vuex-store

一、首先在 store 文件下新建一个index.js文件

const state = () => ({
flag: false
})
const mutations = {
add(state, data) {
state.flag = data
},
}
export default {state, mutations}

二、在页面中调用store里的方法

<p class="sea_school font14" @click="go_sc"> 收藏</p>
 go_sc(){
var token = localStorage.getItem('token')
if(token){
this.getSc()
this.sc = !this.sc
}else{
//去登录_________先去登录才能收藏
this.$message({
message: "请先登录",
type: 'error',
})
    // 本次使用vuex是为了改变登录弹框 flag的值,当点击收藏按钮,未登录时重定向到登录页面
this.$store.commit('add', true)
}
},

三、页面中使用 state中的 flag 值

    <!-- 登录弹框 -->
<div class="mask" v-show="flag">
//....登录弹框的内容省略
</div>
_________________________________________________
<script>
import {mapState} from 'vuex
export defauilt {
computed:{
...mapState(['flag'])
},
} </script>

好啦,大功告成!!

nuxt之vuex的使用的更多相关文章

  1. Nuxt使用Vuex

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 基础知识这里不再重述,学习的话请自行到官网 ...

  2. [Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js

    You often use the same data in different ways across pages. This lesson walks you through setting up ...

  3. [Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js

    You'll begin to notice as you build out your actions in Vuex, many of them will look quite similar. ...

  4. [Nuxt] Load Data from APIs with Nuxt and Vuex

    In a server-rendered application, if you attempt to load data before the page renders and the data f ...

  5. [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js

    You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...

  6. 踩坑记录-nuxt引入vuex报错store/index.js should export a method that returns a Vuex instance.

    错误 store/index.js代码如下: import Vue from 'vue'; import Vuex from 'vuex'; import city from './moudle/ci ...

  7. Nuxt.js项目实战

    感悟 经过几个周六周日的尝试,终于解决了服务端渲染中的常见问题,当SEO不在是问题的时候,或许才是我们搞前端的真正的春天,其中也遇到了一些小坑,Nuxt.js官方还是很给力的,提issue后很积极的给 ...

  8. Nuxt.js使用详解

    首先来讲一下服务端渲染 直白的说就是在服务端拿数据进行解析渲染,直接生成html片段返回给前端.具体用法也有很多种比如: 传统的服务端模板引擎渲染整个页面 服务渲染生成htmll代码块, 前端 AJA ...

  9. vuex分模块3

    nuxt 踩坑之 -- Vuex状态树的模块方式使用 原创 2017年12月20日 11:24:14 标签: vue / nuxt / vuex / 模块化 / 状态管理 874 初次看到这个模块方式 ...

随机推荐

  1. Java程序设计(二)作业

    题目1:输入一个三位十进制数,对其每一位进行筛选,逆序组合后输出. package test; import java.util.*; public class test2{ public stati ...

  2. 220722 T3 石子染色 (背包)

    序列s中的数就是要选的堆的编号,假设要选的有i个石子,这i个染为红色,剩下j个染为蓝色,i+j=x,i=x-j,那么对答案的贡献是|x-2j|.那么只要我们选的有i个石子,贡献就是这么多,所以我们可以 ...

  3. go-zero docker-compose搭建课件服务(四):生成Dockerfile

    0.转载 go-zero docker-compose 搭建课件服务(四):生成Dockerfile并在docker-compose中启动 0.1源码地址 https://github.com/liu ...

  4. go-zero docker-compose 搭建课件服务(七):prometheus+grafana服务监控

    0.转载 go-zero docker-compose 搭建课件服务(七):prometheus+grafana服务监控 0.1源码地址 https://github.com/liuyuede123/ ...

  5. Redis 常见问题-缓存穿透

    问题描述: * 针对 DB 中不存在的数据源,每次请求缓存和数据库都不存在 造成后果: * 应用服务器压力变大 * Redis 命中率大幅度降低 * `数据库压力巨增甚至 down 掉`* 该现象对于 ...

  6. 在ubuntu 上安装golang

    https://golang.google.cn/dl/ 方式一 下载安装包 wget https://golang.google.cn/dl/go1.19.linux-amd64.tar.gz 解压 ...

  7. 几个实用 shell 脚本

    1. Dos攻击防范(自动屏蔽攻击 IP) #!/bin/bash DATE=$(date +%d/%b/%Y:%H:%M) LOG_FILE=/usr/local/nginx/logs/demo2. ...

  8. Oracle数据泵导入dmp文件,报ORA-39083、ORA-01917错误解决办法

    将10.16.23.111数据库服务器中的数据库名称为cwy_init1的数据导出,随后在10.16.23.112数据库服务器创建空库cwy_init2,将导出的cwy_init1数据文件(cwy_i ...

  9. java反序列化cc_link_one2

    CC-LINK-one_second 前言 这条链子其实是上一条链子的另一种走法,在调用危险函数哪里是没有什么变化的 整体链子 还是尾部没有变化嘛还是InvokerTransformer的transf ...

  10. luoguP1505旅游(处理边权的树剖)

    /* luogu1505 非常简单的处理边权的树剖题. 在树上将一条边定向,把这条边的权值赋给这条边的出点 树剖的时候不计算lca权值即可 */ #include<bits/stdc++.h&g ...