前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue3: 如何以 Vite 创建,以 Vue Router, Vuex, Ant Design 开始应用

Vue3: 如何以 Vite 创建,以 Vue Router, Vuex, Ant Design 开始应用

作者头像
GoCoding
发布2021-05-06 14:35:23
7780
发布2021-05-06 14:35:23
举报
文章被收录于专栏:GoCodingGoCoding
  • 本文代码:https://github.com/ikuokuo/start-vue3
  • 在线演示:https://ikuokuo.github.io/start-vue3/ ?

Vite 创建 Vue 3 项目

代码语言:javascript
复制
yarn create vite-app my-site
cd my-site
yarn
yarn dev

运行输出:

代码语言:javascript
复制
❯ yarn dev
yarn run v1.22.10
warning package.json: No license field
$ vite
vite v1.0.0-rc.4
[vite] Optimizable dependencies detected:
vue

  Dev server running at:
  > Local:    http://localhost:3000/
  > Network:  http://192.168.1.100:3000/

访问网址:

与 Vue 2 的差异

详见:Migration Guide, https://v3.vuejs.org/guide/migration/introduction.html

Vite 配置

vite.config.ts:

代码语言:javascript
复制
import { resolve } from "path";

function pathResolve(dir: string) {
  return resolve(__dirname, ".", dir);
}

module.exports = {
  alias: {
    "/@/": pathResolve("src"),
  },
  optimizeDeps: {
    include: ["@ant-design/icons-vue"],
  },
};

详见:Vite - Config File, https://github.com/vitejs/vite#config-file

前提准备

eslint-plugin-vue

代码语言:javascript
复制
yarn add -D eslint eslint-plugin-vue

.eslintrc.js:

代码语言:javascript
复制
module.exports = {
  extends: [
    // add more generic rulesets here, such as:
    // "eslint:recommended",
    "plugin:vue/vue3-recommended",
    // "plugin:vue/recommended" // Use this if you are using Vue.js 2.x.
  ],
  rules: {
    // override/add rules settings here, such as:
    "vue/no-multiple-template-root": "off",
  },
};

TypeScript

代码语言:javascript
复制
yarn add -D typescript

详见:

  • Vue3 - TypeScript https://v3.vuejs.org/guide/typescript-support.html
  • Vite - TypeScript https://github.com/vitejs/vite#typescript

Vue Router

代码语言:javascript
复制
yarn add vue-router@next

Vuex

代码语言:javascript
复制
yarn add vuex@@next

Ant Design Vue

代码语言:javascript
复制
yarn add ant-design-vue@next
# import on demand
yarn add -D babel-plugin-import

# https://github.com/vueComponent/ant-design-vue/issues/2798
yarn add @ant-design/colors

.babelrc:

代码语言:javascript
复制
{
  "plugins": [
    ["import", { "libraryName": "ant-design-vue", "libraryDirectory": "es", "style": "css" }] // `style: true` 会加载 less 文件
  ]
}

其他依赖

代码语言:javascript
复制
yarn add -D sass

开始使用

使用 TypeScript

main.js 重命名为 main.ts

index.html 里把 /src/main.js 替换为 /src/main.ts

使用 Vue Router

router/index.ts:

代码语言:javascript
复制
import { createRouter, createWebHistory } from "vue-router";

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import("/@/views/Home.vue"),
  },
  {
    path: "/setting",
    name: "Setting",
    component: () => import("/@/views/Setting.vue"),
  },
];

export default createRouter({
  history: createWebHistory(),
  routes,
});

使用 Vuex

store/index.ts:

代码语言:javascript
复制
import { createStore } from "vuex";

export default createStore({
  state() {
    return {
      count: 0,
    };
  },
  mutations: {
    increment(state) {
      state.count++;
    },
  },
  actions: {
    increment(context) {
      context.commit("increment");
    },
  },
});

添加 Views

views/Home.vue:

代码语言:javascript
复制
<template>
  <h1>This is a home page</h1>
  <HelloWorld msg="Hello Vue 3.0 + Vite" />
</template>

<script lang="ts">
import { defineComponent } from "vue";

import HelloWorld from "/@/components/HelloWorld.vue";

export default defineComponent({
  name: "Home",
  components: {
    HelloWorld,
  },
});
</script>

views/Setting.vue:

代码语言:javascript
复制
<template>
  <div>
    <h1>This is a setting page</h1>
    <p>store count is: {{ count }}</p>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "Setting",
  computed: {
    count() {
      return this.$store.state.count;
    },
  },
});
</script>

更改 `main.ts`

应用 Vue Router, Vuex, Ant Design :

代码语言:javascript
复制
import { createApp } from "vue";
import router from "/@/router";
import store from "/@/store";

import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";

import App from "/@/App.vue";
import "/@/styles/index.scss";

const app = createApp(App);
app.use(router);
app.use(store);
app.use(Antd);
app.mount("#app");

更改 `App.vue`

使用 Ant Design 搭建首页布局,其中路由部分如下:

代码语言:javascript
复制
<a-layout-header style="background: #fff; padding: 0">
  <a-row type="flex" justify="end">
    <a-col class="mr-3">
      <a-button
        type="primary"
        shape="circle"
        size="large"
        @click="this.$router.push('/')"
      >
        <template #icon><HomeFilled /></template>
      </a-button>
    </a-col>
    <a-col class="mr-3">
      <a-button
        type="primary"
        shape="circle"
        size="large"
        @click="this.$router.push('/setting')"
      >
        <template #icon><SettingFilled /></template>
      </a-button>
    </a-col>
  </a-row>
</a-layout-header>
<a-layout-content style="margin: 0 16px">
  <div :style="{ padding: '24px', background: '#fff', minHeight: '360px' }">
    <router-view />
  </div>
</a-layout-content>

发布到 GitHub Pages

编辑 vite.config.ts 添加 base 路径:

代码语言:javascript
复制
module.exports = {
  // otherwise, may assets 404 or visit with index.html
  base: "/start-vue3/",
  assetsDir: "",
};

编辑 router/index.ts 添加 base 路径:

代码语言:javascript
复制
export default createRouter({
  history: createWebHistory("/start-vue3/"),
  routes,
});

编译,再发布:

代码语言:javascript
复制
cd my-site
yarn build

export GIT_HASH=`git rev-parse --short HEAD`

cd dist/
git init
git remote add origin git@github-ik:ikuokuo/start-vue3.git
git checkout --orphan gh-pages
git add .
git commit -m "deploy website - based on $GIT_HASH"
git push origin gh-pages

访问 https://ikuokuo.github.io/start-vue3/ 体验在线演示。

参考

  • Vue 3 - Docs https://v3.vuejs.org/guide/
  • Ant Design Vue - Docs https://2x.antdv.com/
  • Vue Vben Admin https://github.com/anncwb/vue-vben-admin
  • Deploying a subfolder to GitHub Pages https://gist.github.com/cobyism/4730490

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 GoCoding 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Vite 创建 Vue 3 项目
  • 与 Vue 2 的差异
  • Vite 配置
  • 前提准备
    • eslint-plugin-vue
      • TypeScript
        • Vue Router
          • Vuex
            • Ant Design Vue
              • 其他依赖
              • 开始使用
                • 使用 TypeScript
                  • 使用 Vue Router
                    • 使用 Vuex
                      • 添加 Views
                        • 更改 `main.ts`
                          • 更改 `App.vue`
                          • 发布到 GitHub Pages
                          • 参考
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档