vite2 vue3-ts 添加router, vuex和alias

原文链接: vite2 vue3-ts 添加router, vuex和alias

上一篇: vue3 Set和Map 劫持

下一篇: vite2 ts 搭建webgl开发环境

安装

yarn add vue-router@4
yarn add vuex@next

https://next.vuex.vuejs.org/installation.html

https://github.com/vuejs/vue-router-next

https://next.router.vuejs.org/

https://next.vuex.vuejs.org/installation.html

up-2195ca891ab654901d1d830940af3ff2c8e.png

vuex

store

import { createStore } from "vuex";
const defaultState = {
  count: 0,
};
const store = createStore({
  state() {
    return defaultState;
  },
  mutations: {
    increment(state: typeof defaultState) {
      state.count++;
    },
  },
  getters: {
    double(state: typeof defaultState) {
      return 2 * state.count;
    },
  },
});

export default store;

app.vue

<template>
  <el-button class="flex" @click="add">add</el-button>
  {{ count }}--{{ double }}
</template>

<script lang="ts" setup>
import { useStore } from "vuex";
import { defineComponent, reactive, computed } from "vue";
const store = useStore();
const count = computed(() => store.state.count);
const double = computed(() => store.getters.double);
const add = () => {
  store.commit("increment");
  //  store.dispatch("asyncIncrement");
};
</script>

vue-router

import { createRouter, createWebHashHistory } from "vue-router";
import About from "../components/About.vue";
import MainPage from "../pages/MainPage.vue";
const routes = [
  { path: "/", component: MainPage },
  { name: "about", path: "/about", component: About },
];
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
export default router;

app.vue

<template>
  <el-button class="flex" @click="add">add</el-button>
  {{ count }}--{{ double }}
  <router-view></router-view>
</template>

<script lang="ts" setup>
import { useStore } from "vuex";
import { defineComponent, reactive, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
const store = useStore();
const count = computed(() => store.state.count);
const router = useRouter();
const route = useRoute();
const double = computed(() => store.getters.double);
const add = () => {
  store.commit("increment");
  console.log(route, router, route?.query);
  router.push({
    name: "about",
    query: {
      ...route.query,
    },
  });
};
</script>

alias

方便使用, 但是会失去跳转...


import { resolve } from "path";

  alias: {
    "@": resolve(__dirname, "src"),
  },



import "@/config/index";
import store from "@/store";
import router from "@/router";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值