0 ref
1 写在 组件上
<HelloWorld ref="my_hello_world"></HelloWorld>
2 在父组件中使用:js
handleClick() {
console.log(this.$refs)
this.$refs.my_hello_world.name
this.$refs.my_hello_world.showName()
this.name = this.$refs.my_hello_world.returnName()
}
1 props
props: ['msg'],
props: {
msg: {
type: String, //类型
}
},
props: {
msg: {
type: String, //类型
required: true, //必要性
default: '老王' //默认值
}
},
2 混入
created,methods,data.....
全局每个页面都要写东西:
beforeCreate:启动一个定时器,每隔1s向后端发送一次请求
destroyd:中销毁定时器
1 新建 mixin/index.js
2 写代码:能写的配置项:
data,methods,watch....
3 在组件中局部使用(如果有多个,在数组中继续追加即可)
import common from "@/common";
export default {
name: 'HomeView',
mixins: [common],
}
4 全局使用:main.js中
import common from '@/common'
Vue.mixin(common)
5 以后再在组件中写 data,methods,新的不会影响,如果跟混入一样的会使用当前组件中得
3 插件
功能:用于增强Vue
本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传递的数据
vue-router
vuex
elementui
class Person:
pass
Person.name='lqz'
p=Person()
print(p.name)
new Vue()
Vue.prototype.$name='lqz'
this.$name
-1 plugins/index.js,写入代码
export default {
install(Vue) {
console.log('######' + Vue)
Vue.prototype.$http = axios
Vue.prototype.$add = (a, b) => {
return a + b + 100
}
Vue.mixin({
data(){
return {
name:'lqz'
}
},
methods:{
showName(){
alert(this.name)
}
}
})
}
}
-2 使用插件--main.js中
import plugin from '@/plugins'
Vue.use(plugin)
-vue-router:
-Vue.use(VueRouter)
-以后在组件中this.$router 就拿到的是VueRouter 对象
-以后在组件中 能拿到 this.$route
-全局组件: <router-view/>
-elementui:
-Vue.use(Elementui)
-全局组件: <el-button></el-button>
-全局变量:this.$message()
4 插槽使用
-子组件中:
<template>
<div class="hello">
<button @click="handleClick">组件--点我看控制台</button>
<div>
<slot></slot>
</div>
</template>
-父组件中:
<HelloWorld>
<img src="../assets/img/1.png" alt="" width="200px" height="300px">
</HelloWorld>
<HelloWorld>
<template>
<img src="../assets/img/1.png" alt="" width="200px" height="300px">
</template>
</HelloWorld>
-子组件中:
<template>
<div class="hello">
<button @click="handleClick">组件--点我看控制台</button>
<div>
<slot name='lqz'></slot>
</div>
</template>
-父组件中:
<HelloWorld>
<img src="../assets/img/1.png" alt="" width="200px" height="300px" slot='lqz'>
</HelloWorld>
<HelloWorld>
<template v-slot:lqz>
<img src="../assets/img/1.png" alt="" width="200px" height="300px">
</template>
</HelloWorld>
4 vuex
- 自定义属性
- 自定义事件
- ref属性:this.$refs.名字 拿到组件对象
父组件中:this.$refs.名字 拿到子组件对象
子组件中:this.$parent 拿到父组件对象
-vuex实现组件间通信---》不需要关注父子关系
1 在store/index.js, 写代码---》详情见项目
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 0
},
getters: {},
mutations: {},
actions: {},
modules: {}
})
2 在main.js中引入---》以后 任意组件中都会有 this.$store
import store from './store'
new Vue({
store,
render: h => h(App)
}).$mount('#app')
3 在index.js 的state中定义变量--上面定义过了
state: {
count: 0
},
4 在任意组件中:取值,修改值--->任意组件都可以操作,操作的是同一个变量
js:this.$store.state.count
html中:{{$store.state.count}}
this.$store.state.count++
this.$store.dispatch('addCountAction')
this.$store.commit('addCountMutations')
5 vue-router
页面跳转效果
-1 cnpm install -S vue-router
-2 新建router/index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'home',
component: HomeView
},
]
const router = new VueRouter({
mode: 'history',
base: process.env.BASE_URL,
routes
})
export default router
-3 main.js中
import router from './router'
new Vue({
router,
render: h => h(App)
}).$mount('#app')
-4 以后再任意组件中,都有
this.$router
this.$route
-5 在App.vue中
<div id="app">
<router-view/>
</div>
-6 以后只要配置路由和页面组件的对应关系,在浏览器中访问地址即可
-通过js跳转
this.$router.push('/about')
-通过组件跳转
<router-link to="/about">
任意标签
</router-link>
-通过js跳转时,传对象
this.$router.push({
name:'about'
})
this.$router.push({
path:'/about'
})
this.$router.push({
name:'about',
query:{name:'lqz',age:19}
})
this.$route.query.名字
-router/index.js中:
{
path: '/about/:id',
name: 'about',
component: AboutView
}
-组件中跳转
this.$router.push({
name:'about',
params:{id:999}
})
this.$router.push('/about/666')
-在另一个组件中取:
this.$route.params.名字
-通过标签跳转传对象
<router-link :to="to_url"><button>标签跳转-传对象</button></router-link>
to_url:{name:'about',query:{},params:{}}
5.1 路由嵌套–子路由
5.2 相关api
this.$router.push(path): 相当于点击路由链接(可以返回到当前路由界面)
this.$router.replace(path): 用新路由替换当前路由(不可以返回到当前路由界面)
this.$router.back(): 请求(返回)上一个记录路由
this.$router.go(-1): 请求(返回)上一个记录路由
this.$router.go(1): 请求下一个记录路由
5.3 路由守卫
作用:对路由进行权限控制
router.beforeEach((to, from, next) => {
console.log('前置路由守卫', to, from)
if (to.name == 'home' || localStorage.getItem('name')) {
next()
} else {
alert('您没有权限')
}
})
5.4 路由两种工作模式
1 对于一个url来说,什么是hash值?——
2 hash值不会包含在 HTTP 请求中,即:hash值不会带给服务器。
3 hash模式:
地址中永远带着
若以后将地址通过第三方手机app分享,若app校验严格,则地址会被标记为不合法。
兼容性较好。
4 history模式:
地址干净,美观 。 192.168.1.1/login 192.168.1.1/home
兼容性和hash模式相比略差。
应用部署上线时需要后端人员支持,解决刷新页面服务端404的问题
6 前端开源项目
-django-vue-admin:https://gitee.com/liqianglog/django-vue-admin/tree/main/web
-https://gitee.com/yudaocode/yudao-ui-admin-vue2
-https://gitee.com/xany/yoshop2.0-uniapp 商城类
-https://gitee.com/yudaocode/yudao-mall-uniapp
7 localStorage,sessionStorage和cookie的使用
存在cookie中:过期时间,过期了,就清理掉了
localStorage:永久有效,即便浏览器重启也有效,只能手动或代码删除
sessionStorage:当次有效,关闭浏览器,就没了
cnpm install vue-cookies -S
cookie.set('xx', 'yy', '7d')
console.log(cookie.get('xx'))
cookie.remove('xx')
methods: {
saveLocalStorage() {
// localStorage.setItem('name', 'xxx')
// sessionStorage.setItem('name', '彭于')
cookie.set('xx', 'yy', '7d')
},
getLocalStorage() {
// console.log(localStorage.getItem('name'))
// console.log(sessionStorage.getItem('name'))
console.log(cookie.get('xx'))
},
deleteLocalStorage() {
// localStorage.clear()
// localStorage.removeItem('name')
// sessionStorage.clear()
// sessionStorage.removeItem('name')
cookie.remove('xx')
},
}