组件/路由/vuex

组件
局部功能的界面(包括html、css、js、img等)

在这里插入图片描述
自己定义一个组件:例如:1.创建HelloWorld.vue组件

<template>
<div>
    <p class="msg">{{msg}}</p>
</div>
</template>

<script >
export default {// 配置对象(与vue一致)
//声明接收属性,这个属性就会成为组件的属性
props:['comments'],
  data () { // 必须是函数
    return {
      msg: 'Hello Vue'
    }
  }
}
</script>

<style>
.msg {
    color: pink;
    font-size: 30px;
}
</style>

2.App.vue :使用组件三步骤

<template>
    <div>
        <img src="./assets/logo.png" alt="logo" class="logo">
        <!-- 3.使用组件标签 -->
       <!--绑定属性 将属性传给组件 组件间通讯-->
        <HelloWorld :comments='comments'/>
    </div>
</template>

<script>
// 1 引入组件
import HelloWorld from './component/HelloWorld.vue'
export default {
  data () {
	return {
	comments:[
		{name:'bb',say:'vue'}
		]
	}
  },
  // 2.映射组件标签
  components: {
    HelloWorld
  }
}
</script>

<style>
.logo {
    width: 200px;
    height: 200px;
}
</style>

3.创建main.js 作为入口js 创建实例vm对象

/*
入口js:创建Vue实例
放第一个Vue大写 第二个小写
*/
import Vue from 'vue'
import App from './App.vue'
// eslint-disable-next-line
/* eslint-disable */
new Vue({
    el: '#app',
    //参考index.html
    components: {
        App
    },
    template: '<App/>'
})

打包之后生成dist

vm.$emit(‘绑定监听的名字’,参数):触发自定义事件 传数据

<div id="emit-example-simple">
  <welcome-button v-on:welcome="sayHi"></welcome-button>
</div>
<script>
Vue.component('welcome-button', {
  template: `
    <button v-on:click="$emit('welcome')">
      Click me to be welcomed
    </button>
  `
})
  new Vue({
  el: '#emit-example-simple',
  methods: {
    sayHi: function () {
      alert('Hi!')
    }
  }
})
</script>

mounted:{//执行异步代码}
//绑定事件监听—订阅消息
触发事件-----发布消息

Pubsub订阅发布消息 :传数据
props:得在父子之间传递

作用:用于传递数据(可以是兄弟间 父子间)

优点:组件之间不需要任何关系

<script>
	//订阅
import Pubsub from 'pubsub-js'
import {pubsubID} from 'xxx'

componentDidMount() {
	/**
	* 订阅
	*/
	pubsub = Pubsub.subscribe(pubsubID, (msg, data) => {
		console.log(msg) // 这里将会输出对应设置的 pubsubID ,msg参数不能去掉
		console.log(data) // 这里将会输出对应设置的参数
	})
}

//发布
import Pubsub from 'pubsub-js'
Pubsub.publish(pubsubID, data) //data是传递给subscribe的data数据
// 或
PubSub.publishSync(StationStatisticsID, data)

</script>

reduce()对数字实现累加

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15

组件通信slot:此方式用于父组件向子组件传递‘标签数据’

localStorage存储数据:暴露的是函数还是对象看要向外暴露的功能是1个还是多个

vue-resource

// 引入模块
import VueResource from 'vue-resource'
// 使用插件
Vue.use(VueResource)
//内部会给vm对象和组件对象添加一个属性:$http
// 通过 vue/组件对象发送 ajax 请求
this.$http.get('/someUrl').then((response) => {
// success callback
console.log(response.data) //返回结果数据
}, (response) => {
// error callback

axios请求

// 引入模块 在哪里使用在哪里引入

import axios from 'axios'
// 发送 ajax 请求
axios.get(url)
.then(response => {
console.log(response.data) // 得到返回结果数据
})
.catch(error => {
console.log(error.message)
})

路由
key----value
key对应path-----后端返回的value是:处理请求的回调函数/前端 前端:组件
1.优化路由配置:linkActiveClass: ‘active’, // 指定选中的路由链接的 class
2.使用路由的三步骤:
1)定义路由组件
2)注册路由

export default new VueRouter({
routes: [
{
path: '/',
redirect: '/about'
},
{
path: '/about',
component: About,
meta:{
	isShow:true;
	//meta里面可以存放一些属性值 通过$route.meta.isShow可以获取
	}
},
{
path: '/home',
component: Home
}
]
})

//注册路由器
import Vue from 'vue'
import router from './router'
// 创建 vue 配置路由器
new Vue({
el: '#app',
router,
render: h => h(app)
})

3)使用路由:

缓存路由组件对象
编码实现:


在js中跳转页面 称为 编程式路由导航
window.location=url;//跳转到一个页面

r o u t e 和 route和 routerouter 的区别
Vue中的this分为 r o u t e 和 route和 routerouter
$route:是代表当前组件路由:存了一些数据 例如parmas和query等参数
$router:代表路由器:存放一些功能方法 例如:push replace back等相关 API

相关API:1) this. r o u t e r . p u s h ( p a t h ) : 相 当 于 点 击 路 由 链 接 ( 可 以 返 回 到 当 前 路 由 界 面 ) 2 ) t h i s . router.push(path): 相当于点击路由链接(可以返回到当前路由界面) 2) this. router.push(path):()2)this.router.replace(path): 用新路由替换当前路由(不可以返回到当前路由界面)
3) this. r o u t e r . b a c k ( ) : 请 求 ( 返 回 ) 上 一 个 记 录 路 由 4 ) t h i s . router.back(): 请求(返回)上一个记录路由 4) this. router.back():()4)this.router.go(-1): 请求(返回)上一个记录路由
5) this.$router.go(1): 请求下一个记录路由

将一个伪数组转成真数组
Array.prototype.slice.call() : 用call来继承数组的方法slice
数组查询之后如果没有值则都返回undefined

属性描述符
在这里插入图片描述

documentFragement 也是一个node节点
在这里插入图片描述
界面不变

Dep和Watcher之间的关系
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

vuex
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于运动装备管理系统的问题。我可以为您解答。 在Vue.js中,可以使用Vue Router来实现路由功能,使用Vuex来管理应用程序的状态。对于添加运动装备到购物车的功能,我们可以在Vuex的store中创建一个状态,用来存储购物车中的所有运动装备。当用户点击“加入购物车”按钮时,我们可以向store中的购物车状态中添加该商品并弹出提示信息。 首先,在store中定义购物车状态,可以使用数组来存储所有的运动装备: ``` //store.js const state = { cartItems: [] } ``` 然后,在mutations中定义一个方法,用来将商品添加到购物车中: ``` //store.js const mutations = { addToCart(state, item) { state.cartItems.push(item) } } ``` 接下来,在组件中,我们需要使用mapMutations将addToCart方法映射到组件中,然后在点击“加入购物车”按钮时,调用该方法将商品添加到购物车中: ``` //MyComponent.vue <template> <div> <button @click="addItemToCart">加入购物车</button> </div> </template> <script> import { mapMutations } from 'vuex' export default { methods: { ...mapMutations(['addToCart']), addItemToCart() { const item = { id: 1, name: '运动装备' } //这里只是示例,实际上应该从接口获取商品信息 this.addToCart(item) alert('加入购物车成功') } } } </script> ``` 最后,在路由中定义购物车页面,并在该页面中显示购物车中的所有商品: ``` //router.js import Cart from './components/Cart.vue' const routes = [ { path: '/cart', component: Cart } ] export default routes ``` ``` //Cart.vue <template> <div> <h1>购物车</h1> <ul> <li v-for="item in cartItems" :key="item.id">{{ item.name }}</li> </ul> </div> </template> <script> import { mapState } from 'vuex' export default { computed: { ...mapState(['cartItems']) } } </script> ``` 以上就是实现运动装备管理系统中“加入购物车”功能的基本步骤。希望能对您有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值