Vue记录(下篇)

news2024/10/6 16:25:29

Vuex

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

getters配置项

*Count.vue

<template>
	<div>
		<h1>当前求和为:{{$store.state.sum}}</h1>
		<h3>当前求和的10倍为:{{$store.getters.bigSum}}</h3>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment">+</button>
		<button @click="decrement">-</button>
		<button @click="incrementOdd">当前求和为奇数再加</button>
		<button @click="incrementWait">等一等再加</button>
	</div>
</template>

<script>
	export default {
		name:'Count',
		data() {
			return {
				n:1, //用户选择的数字
			}
		},
		methods: {
			increment(){
				this.$store.commit('ADD',this.n)
			},
			decrement(){
				this.$store.commit('SUBTRACT',this.n)
			},
			incrementOdd(){
				this.$store.dispatch('addOdd',this.n)
			},
			incrementWait(){
				this.$store.dispatch('addWait',this.n)
			},
		},
	}
</script>

<style>
	button{
		margin-left: 5px;
	}
</style>

index.js

//引入Vue核心库
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)
   
//准备actions对象——响应组件中用户的动作
const actions = {
    addOdd(context,value){
        console.log("actions中的addOdd被调用了")
        if(context.state.sum % 2){
            context.commit('ADD',value)
        }
    },
    addWait(context,value){
        console.log("actions中的addWait被调用了")
        setTimeout(()=>{
			context.commit('ADD',value)
		},500)
    },
}
//准备mutations对象——修改state中的数据
const mutations = {
    ADD(state,value){
        state.sum += value
    },
    SUBTRACT(state,value){
        state.sum -= value
    }
}
//准备state对象——保存具体的数据
const state = {
    sum:0 //当前的和
}
//准备getters对象——用于将state中的数据进行加工
const getters = {
    bigSum(){
        return state.sum * 10
    }
}
   
//创建并暴露store
export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})

getters配置项的使用:

  1. 概念:当state中的数据需要经过加工后再使用时,可以使用getters加工

  2. store.js中追加getters配置

const getters = {
	bigSum(state){
		return state.sum * 10
	}
}

//创建并暴露store
export default new Vuex.Store({
	...
	getters
})
  1. 组件中读取数据:$store.getters.bigSum

四个map方法的使用

mapState与mapGetters

index.js

//引入Vue核心库
import Vue from 'vue'
//引入Vuex
import Vuex from 'vuex'
//应用Vuex插件
Vue.use(Vuex)
   
//准备actions对象——响应组件中用户的动作
const actions = {
    addOdd(context,value){
        console.log("actions中的addOdd被调用了")
        if(context.state.sum % 2){
            context.commit('ADD',value)
        }
    },
    addWait(context,value){
        console.log("actions中的addWait被调用了")
        setTimeout(()=>{
			context.commit('ADD',value)
		},500)
    },
}
//准备mutations对象——修改state中的数据
const mutations = {
    ADD(state,value){
        state.sum += value
    },
    SUBTRACT(state,value){
        state.sum -= value
    }
}
//准备state对象——保存具体的数据
const state = {
    sum:0, //当前的和
    name:'JOJO',
    school:'尚硅谷',
}
//准备getters对象——用于将state中的数据进行加工
const getters = {
    bigSum(){
        return state.sum * 10
    }
}
   
//创建并暴露store
export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})

Count.vue

<template>
	<div>
		<h1>当前求和为:{{sum}}</h1>
		<h3>当前求和的10倍为:{{bigSum}}</h3>
		<h3>我是{{name}},我在{{school}}学习</h3>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment">+</button>
		<button @click="decrement">-</button>
		<button @click="incrementOdd">当前求和为奇数再加</button>
		<button @click="incrementWait">等一等再加</button>
	</div>
</template>

<script>
	import {mapState,mapGetters} from 'vuex'

	export default {
		name:'Count',
		data() {
			return {
				n:1, //用户选择的数字
			}
		},
		methods: {
			increment(){
				this.$store.commit('ADD',this.n)
			},
			decrement(){
				this.$store.commit('SUBTRACT',this.n)
			},
			incrementOdd(){
				this.$store.dispatch('addOdd',this.n)
			},
			incrementWait(){
				this.$store.dispatch('addWait',this.n)
			},
		},
		computed:{		
			// 借助mapState生成计算属性(数组写法)
			// ...mapState(['sum','school','name']),
			// 借助mapState生成计算属性(对象写法)
			...mapState({sum:'sum',school:'school',name:'name'}),

			...mapGetters(['bigSum'])
		}
	}
</script>

<style>
	button{
		margin-left: 5px;
	}
</style>

mapState方法:用于帮助我们映射state中的数据

computed: {
    //借助mapState生成计算属性:sum、school、subject(对象写法)
     ...mapState({sum:'sum',school:'school',subject:'subject'}),
         
    //借助mapState生成计算属性:sum、school、subject(数组写法)
    ...mapState(['sum','school','subject']),
},

mapGetters方法:用于帮助我们映射getters中的数据

computed: {
    //借助mapGetters生成计算属性:bigSum(对象写法)
    ...mapGetters({bigSum:'bigSum'}),

    //借助mapGetters生成计算属性:bigSum(数组写法)
    ...mapGetters(['bigSum'])
},

mapActions与mapMutations

Count.vue

<template>
	<div>
		<h1>当前求和为:{{sum}}</h1>
		<h3>当前求和的10倍为:{{bigSum}}</h3>
		<h3>我是{{name}},我在{{school}}学习</h3>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment(n)">+</button>
		<button @click="decrement(n)">-</button>
		<button @click="incrementOdd(n)">当前求和为奇数再加</button>
		<button @click="incrementWait(n)">等一等再加</button>
	</div>
</template>

<script>
	import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'

	export default {
		name:'Count',
		data() {
			return {
				n:1, //用户选择的数字
			}
		},
		methods: {
			// 借助mapActions生成:increment、decrement(对象形式)
			...mapMutations({increment:'ADD',decrement:'SUBTRACT'}),

			// 借助mapActions生成:incrementOdd、incrementWait(对象形式)
			...mapActions({incrementOdd:'addOdd',incrementWait:'addWait'})
		},
		computed:{		
			// 借助mapState生成计算属性(数组写法)
			// ...mapState(['sum','school','name']),
			// 借助mapState生成计算属性(对象写法)
			...mapState({sum:'sum',school:'school',name:'name'}),

			...mapGetters(['bigSum'])
		}
	}
</script>

<style>
	button{
		margin-left: 5px;
	}
</style>

mapActions方法:用于帮助我们生成与actions对话的方法,即:包含$store.dispatch(xxx)的函数

methods:{
    //靠mapActions生成:incrementOdd、incrementWait(对象形式)
    ...mapActions({incrementOdd:'jiaOdd',incrementWait:'jiaWait'})

    //靠mapActions生成:incrementOdd、incrementWait(数组形式)
    ...mapActions(['jiaOdd','jiaWait'])
}

mapMutations方法:用于帮助我们生成与mutations对话的方法,即:包含$store.commit(xxx)的函数

=

备注:mapActions与mapMutations使用时,若需要传递参数,则需要在模板中绑定事件时传递好参数,否则参数是事件对象

vue-router路由

在这里插入图片描述
在这里插入图片描述
下载vue-router:npm i vue-router

基本路由

  1. 安装vue-router,命令:npm i vue-router
  2. 应用插件:Vue.use(VueRouter)
  3. 编写router配置项:
//引入VueRouter 
import VueRouter from 'vue-router' 
//引入Luyou 组件 
import About from '../components/About' import Home from '../components/Home'

//创建router实例对象,去管理一组一组的路由规则 
const router = new VueRouter({ 	
routes:[ 		
{ 			
path:'/about', 			
component:About 		
}, 		
{ 			
path:'/home', 			
component:Home 		
} 	
] 
})
//暴露router export default router
实现切换(active-class可配置高亮样式):
<router-link active-class="active" to="/about">About</router-link> 1 指定展示位:<router-view></router-view>

嵌套路由

  1. 配置路由规则,使用children配置项:
routes:[
	{
		path:'/about',
		component:About,
	},
	{
		path:'/home',
		component:Home,
		children:[ //通过children配置子级路由
			{
				path:'news', //此处一定不要写:/news
				component:News
			},
			{
				path:'message', //此处一定不要写:/message
				component:Message
			}
		]
	}
]
  1. 跳转(要写完整路径):News

路由的query参数

  1. 传递参数:
<!-- 跳转并携带query参数,to的字符串写法 -->
<router-link :to="/home/message/detail?id=666&title=你好">跳转</router-link>
				
<!-- 跳转并携带query参数,to的对象写法 -->
<router-link :to="{
	path:'/home/message/detail',
	query:{
		id:666,
        title:'你好'
	}
}">跳转</router-link>
  1. 接收参数:
$route.query.id
$route.query.title

命名路由

  1. 作用:可以简化路由的跳转
  2. 给路由命名:
{
	path:'/demo',
	component:Demo,
	children:[
		{
			path:'test',
			component:Test,
			children:[
				{
                    name:'hello' //给路由命名
					path:'welcome',
					component:Hello,
				}
			]
		}
	]
}
  1. 简化跳转:
<!--简化前,需要写完整的路径 -->
<router-link to="/demo/test/welcome">跳转</router-link>

<!--简化后,直接通过名字跳转 -->
<router-link :to="{name:'hello'}">跳转</router-link>

<!--简化写法配合传递参数 -->
<router-link 
	:to="{
		name:'hello',
		query:{
		    id:666,
            title:'你好'
		}
	}"
>跳转</router-link>

路由的params参数

  1. 配置路由,声明接收params参数:
{
	path:'/home',
	component:Home,
	children:[
		{
			path:'news',
			component:News
		},
		{
			component:Message,
			children:[
				{
					name:'xiangqing',
					path:'detail/:id/:title', //使用占位符声明接收params参数
					component:Detail
				}
			]
		}
	]
}
  1. 传递参数:
<!-- 跳转并携带params参数,to的字符串写法 -->
<router-link :to="/home/message/detail/666/你好">跳转</router-link>
				
<!-- 跳转并携带params参数,to的对象写法 -->
<router-link 
	:to="{
		name:'xiangqing',
		params:{
		   id:666,
            title:'你好'
		}
	}"

>跳转</router-link>

特别注意:路由携带params参数时,若使用to的对象写法,则不能使用path配置项,必须使用name配置!

  1. 接收参数:
$route.params.id
$route.params.title

路由的props配置

作用:让路由组件更方便的收到参数

{
	name:'xiangqing',
	path:'detail/:id',
	component:Detail,

	//第一种写法:props值为对象,该对象中所有的key-value的组合最终都会通过props传给Detail组件
	// props:{a:900}

	//第二种写法:props值为布尔值,布尔值为true,则把路由收到的所有params参数通过props传给Detail组件
	// props:true
	
	//第三种写法:props值为函数,该函数返回的对象中每一组key-value都会通过props传给Detail组件
	props(route){
		return {
			id:route.query.id,
			title:route.query.title
		}
	}
}

路由跳转的replace方法

  1. 作用:控制路由跳转时操作浏览器历史记录的模式
  2. 浏览器的历史记录有两种写入方式:push和replace,其中push是追加历史记录,replace是替换当前记录。路由跳转时候默认为push方式
    开启replace模式:<router-link replace …>News

编程式路由导航

作用:不借助实现路由跳转,让路由跳转更加灵活

具体编码:

this.$router.push({
	name:'xiangqing',
    params:{
        id:xxx,
        title:xxx
    }
})

this.$router.replace({
	name:'xiangqing',
    params:{
        id:xxx,
        title:xxx
    }
})
this.$router.forward() //前进
this.$router.back() //后退
this.$router.go() //可前进也可后退

缓存路由组件

作用:让不展示的路由组件保持挂载,不被销毁

具体编码:

//缓存一个路由组件
<keep-alive include="News"> //include中写想要缓存的组件名,不写表示全部缓存
    <router-view></router-view>
</keep-alive>

//缓存多个路由组件
<keep-alive :include="['News','Message']"> 
    <router-view></router-view>
</keep-alive>

activated和deactivated

activateddeactivated是路由组件所独有的两个钩子,用于捕获路由组件的激活状态
具体使用:
activated路由组件被激活时触发
deactivated路由组件失活时触发
在这里插入图片描述

UI组件库

在这里插入图片描述
安装 element-ui:npm i element-ui -S
main.js:

import Vue from 'vue'
import App from './App.vue'
//引入ElementUI组件库
import ElementUI from 'element-ui';
//引入ElementUI全部样式
import 'element-ui/lib/theme-chalk/index.css';

Vue.config.productionTip = false
//使用ElementUI
Vue.use(ElementUI)

new Vue({
    el:"#app",
    render: h => h(App),
})

安装 babel-plugin-component:npm install babel-plugin-component -D

修改 babel-config-js:

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset',
    ["@babel/preset-env", { "modules": false }]
  ],
  plugins: [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1017879.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

150.逆波兰表达式求值

目录 一、题目 二、分析代码 三、中缀表达式转后缀表达式 一、题目 150. 逆波兰表达式求值 - 力扣&#xff08;LeetCode&#xff09; 二、分析代码 class Solution { public:int evalRPN(vector<string>& tokens) {stack<int>s;for(auto ch:tokens){if(ch!…

小白备战大厂算法笔试(九)——九大排序算法

文章目录 排序选择排序冒泡排序插入排序快速排序基准数优化尾递归优化 归并排序堆排序桶排序计数排序基数排序排序算法对比 排序 评价维度&#xff1a; 运行效率&#xff1a;我们期望排序算法的时间复杂度尽量低&#xff0c;且总体操作数量较少&#xff08;即时间复杂度中的常…

基于Yolov8的交通标志牌(TT100K)识别检测系统

1.Yolov8介绍 Ultralytics YOLOv8是Ultralytics公司开发的YOLO目标检测和图像分割模型的最新版本。YOLOv8是一种尖端的、最先进的&#xff08;SOTA&#xff09;模型&#xff0c;它建立在先前YOLO成功基础上&#xff0c;并引入了新功能和改进&#xff0c;以进一步提升性能和灵活…

在gazebo仿真环境中加载多个机器人

文章目录 前言一、基本概念1、xacro2、Gazebo 加载单个机器人模型 二、原先launch文件代码三、 修改launch文件加载多个机器人总结 前言 单个机器人的各项仿真实验都基本完成&#xff0c;也实现了远程控制&#xff0c;接下来主要对多机器人编队进行仿真实验&#xff0c;在进行…

Git 命令图形化在线练习

git 命令在线练习网址如下: http://onlywei.github.io/explain-git-with-d3/ 在master上先提交2个commit,创建3个分支,分支1打5个commit,分支2打6commit ,分支3commit,master分支打9commit. git commit -m "master c 1" git commit -m "master c 1"git …

程序员必备神器:He3万能工具箱全解析

He3是一个为前端、后端开发者打造的终极工具箱软件&#xff0c;提供了近400的功能&#xff0c;将开发效率提升到一个新的水平。。本文将介绍 He3 开发者工具箱的主要功能和特点。 He3包含Web版和客户端&#xff0c;客户端支持windows和mac。下载后即可使用非常方便。 先睹为快…

FL Studio v21.1.1.3750 Producer Edition inc crack官方中文免费激活版功能介绍及百度网盘下载

FL Studio v21.1.1.3750 Producer Edition inc crack官方中文免费激活版是一款功能强大的软件音乐制作环境或数字音频工作站&#xff08;DAW&#xff09;。它代表了25多年的创新发展&#xff0c;在一个软件包中拥有您所需的一切&#xff0c;以创作、编排、录制、编辑、混音和掌…

C++之vector元素访问函数operator[]、at、front、back、data总结(二百零三)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

如何在 Excel 中计算日期之间的天数

计算两个日期之间的天数是 Excel中的常见操作。无论您是规划项目时间表、跟踪时间还是分析一段时间内的趋势&#xff0c;了解如何在 Excel 中查找日期之间的天数都可以提供强大的日期计算功能。 幸运的是&#xff0c;Excel 提供了多种简单的方法来获取两个日期之间的天数。继续…

Javascript EventListener 事件监听 (mouseover、mouseout)

事件指的是在html元素上发生的事情&#xff0c;例如图片元素被点击事件触发时&#xff0c;可设置执行一段js代码。对事件作出反应&#xff0c;通过元素的事件属性&#xff0c;启用事件监听器。 事件监听器是指 addEventListener (给DOM对象添加事件处理程序) 和 removeEventLis…

441分2023级东南大学920专业基础综合信号和数字电路考研上岸经验分享信息科学与工程学院

写在前面的话 本人是23年考生&#xff0c;本科就读于西电电子信息工程&#xff0c;以441分总分&#xff08;数学一149&#xff0c;英语83&#xff0c;专业课137&#xff0c;政治73&#xff09;考上东南信院电路与系统专业。以下所言皆是考研历程中的重要感悟&#xff0c;因为一…

c语言练习61:malloc和free

malloc和free malloc C语⾔提供了⼀个动态内存开辟的函数&#xff1a; 1 void* malloc (size_t size); 这个函数向内存申请⼀块连续可⽤的空间&#xff0c;并返回指向这块空间的指针。 • 如果开辟成功&#xff0c;则返回⼀个指向开辟好空间的指针。 • 如果开辟失败&…

mysql的变量

在 MySQL 中变量分为三种类型 : 系统变量、用户定义变量、局部变量。 系统变量 系统变量 是 MySQL 服务器提供&#xff0c;不是用户定义的&#xff0c;属于服务器层面。分为全局变量&#xff08; GLOBAL &#xff09;、会话变量&#xff08;SESSION &#xff09;。 查看系统变…

选择适合您网站的SSL证书,保障安全与信任

在如今数字化的时代&#xff0c;拥有一个安全可靠的网站是至关重要的。而SSL证书作为保护网站和用户数据安全的关键工具&#xff0c;选择适合自己网站的SSL证书成为了每个网站管理员必须面对的重要任务。下面将为您分享几个关键因素&#xff0c;帮助您做出明智的选择。 1. 网站…

Linux的调试工具 - gdb(超详细)

Linux的调试工具 - gdb 1. 背景2. 开始使用指令的使用都用下面这个C语言简单小代码来进行演示&#xff1a;1. list或l 行号&#xff1a;显示文件源代码&#xff0c;接着上次的位置往下列&#xff0c;每次列10行。2. list或l 函数名:列出某个函数的源代码。3. r或run: 运行程序。…

链队列的基本操作(带头结点,不带头结点)

结构体 typedef struct linknode{int data;struct linknode* next;后继指针 }linknode; typedef struct {linknode* front, * rear;//队头队尾指针 }linkquene; 初始化队列&#xff08;带头结点&#xff09; int initquene(linkquene* q)//初始化队列 {q->front q->r…

geant4创建自己的physicslist(以电磁物理为例)

1 基本概念 1.1 需要创建一个类继承 G4ModularPhysicist 每个physics都是继承 G4PhysicsConstruct Physicslist 由很多 physics组成&#xff0c;physics里面包含很多的process,也就是物理过程,光电效应就是一个process 1.2 model的概念:实现proces,一个process可以对应多个…

阿里云无影云电脑有什么用?常用使用场景说明

阿里云无影云电脑是一种易用、安全、高效的云上桌面服务&#xff0c;阿里云无影云电脑可用于高数据安全管控、高性能计算等要求的金融、设计、视频、教育等领域&#xff0c;适用于多种办公场景&#xff0c;如远程办公、多分支机构、安全OA、短期使用、专业制图等。阿里云百科来…

ES6(二)

文章目录 对象的扩展对象的展开运算符Object.is()Object.assign() 字符串的扩展includes(), startsWith(), endsWith()repeat()padStart()&#xff0c;padEnd()trimStart()&#xff0c;trimEnd() 运算符扩展指数运算符 Set方法应用 Map方法 对象的扩展 ES6 允许在大括号里面&a…

DC系列靶机5通关教程

信息收集 主机扫描 sudo arp-scan -l端口扫描 nmap -p- -A 192.168.16.172漏洞发现 浏览器访问靶机IP 在Contact找到类似提交数据的地方 点击submit&#xff0c;数字发生变化。不断刷新的话&#xff0c;数字依然会发生变化 使用bp抓包发送重发器查看数据包 再次点击发送查看…