-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【Vue 2.X】学习笔记 #7
Comments
vue-router |
vuex |
vue 经验分享 |
Vue 开发规范[TOC] 结构化规范目录文件夹及子文件规范
组件规范组件选项顺序
<template>
<div>
// 内容
</div>
</template>
<script>
export default {
// 组件名
name: 'MyComponent',
components: {
// 组件
},
directives: {
// 指令
},
filters: {
// 过滤器
},
mixins: [
// 混合
],
props: {
// 属性
},
data: {
// 本地状态
},
computed: {
// 计算属性
},
watch: {
// 侦听器
}
// 生命周期函数
beforeCreate () {
// 创建前
},
created () {
// 创建后
},
beforeMount () {
// 挂载前
},
mounted () {
// 挂载后
},
beforeUpdate () {
// 更新前
},
updated () {
// 更新后
},
activated () {
// 激活时
},
deactivated () {
// 停用时
},
beforeDestroy () {
// 销毁前
},
destroyed () {
// 销毁后
},
methods: {
// 方法
},
render () {
// 渲染
}
}
</script>
<style lang="scss" scoped>
.component {
//
}
</style> 避免
|
路由动态过渡<template>
<transition :name="transitionName">
<router-view class="root-router-view" />
</transition>
</template>
<script>
export default {
name: 'app',
data() {
return {
transitionName: 'route-left'
}
},
beforeRouteEnter(to, from, next) {
const toDepth = to.path.split('/').length
const fromDepth = from.path.split('/').length
const transitionName = toDepth < fromDepth ? 'route-right' : 'route-left'
next(vm => {
vm.transitionName = transitionName
})
}
}
</script>
<style>
.root-router-view{
position: fixed !important;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.route-left-enter,
.route-right-leave-active {
transform: translate3d(100%, 0, 0);
}
.route-left-leave-active,
.route-right-enter {
transform: translate3d(-100%, 0, 0);
}
.route-left-enter-active,
.route-left-leave-active,
.route-right-enter-active,
.route-right-leave-active {
transition: transform 0.4s ease;
}
</style> |
组件中注册动态vuex模块参考:
// user store.js
export const NAMESPACE = '__USER'
const store = {
namespaced: true,
state: {
user: null
},
getters: {
name(state) {
return (state.user && state.user.name) || ''
}
},
mutations: {
SET_USER(state, value) {
state.user = value
}
},
actions: {
async queryUser({ commit }) {
const res = await fetch('/api/users/110')
commit('SET_USER', res.data)
return res
}
}
}
export default {
install(ctx) {
if (ctx.$store.state[NAMESPACE]) {
return console.log(`${NAMESPACE} has been installed`)
}
ctx.$store.registerModule(NAMESPACE, store)
},
uninstall(ctx) {
if (ctx.$store.state[NAMESPACE]) {
ctx.$store.unregisterModule(NAMESPACE)
}
}
} 例子 // user.vue
import Store, { NAMESPACE } from './store'
export default {
computed: {
user() {
return this.$store.state[NAMESPACE].user
},
username() {
return this.$store.getters[NAMESPACE].name
}
},
beforeCreate() {
Store.install(this)
this.$once('hook:beforeDestroy', () => {
Store.uninstall(this)
})
},
methods: {
getUser() {
this.$store.dispatch(NAMESPACE + '/queryUser')
}
}
} |
No description provided.
The text was updated successfully, but these errors were encountered: