vue3中router路由和vuex的store使用方法
1.路由<script>import { useRouter, useRoute } from 'vue-router'export default {setup() {const router = useRouter() // 组件内路由const route = useRoute() // 组件内路由信息}}</script>2.vuex创建storeimport {
·
1.路由
<script>
import { useRouter, useRoute } from 'vue-router'
export default {
setup() {
const router = useRouter() // 组件内路由
const route = useRoute() // 组件内路由信息
}
}
</script>
2.vuex
- 创建store
import { createStore } from 'vuex'
import login from './login'
const store = createStore({
state: {},
mutations: {},
actions: {},
modules: {
login
}
})
export default store
- 组件内使用store
<script>
import { useStore } from 'vuex'
export default {
setup() {
const store = useStore()
const state = store.state
const methods = {
// 处理commit
handleMutation: () => {
store.commit(...)
},
// 处理dispatch
handleAction: () => {
store.dispatch(...)
}
}
return {
...methods
}
}
}
</script>
更多推荐
已为社区贡献7条内容
所有评论(0)