《Asp.Net Core3 + Vue3入坑教程》 此教程適合新手入門或者先後端分離嘗試者。能夠根據圖文一步一步進操做編碼也能夠選擇直接查看源碼。每一篇文章都有對應的源碼css
Asp.Net Core後端項目html
Vue3 前端項目前端
本文爲《Asp.Net Core3 + Vue3入坑教程》系列教程的前端第二篇 - 使用Ant Design of Vue編寫頁面 & vue-router 初試。本文接着上文《使用vue-cli建立vue項目》新建的simple-vue的基礎上將使用Ant Design of Vue建立簡單頁面,並結合vue-router實現頁面之間的簡單跳轉,具體效果以下:vue
Ant Design of Vue 官方文檔 https://2x.antdv.com/docs/vue/getting-started-cnreact
Vetur插件簡介 https://marketplace.visualstudio.com/items?itemName=octref.veturgit
在終端執行命令:github
yarn add ant-design-vue@next
代碼以下:vue-router
import { createApp } from 'vue' import App from './App.vue' import Antd from 'ant-design-vue'; import 'ant-design-vue/dist/antd.css'; createApp(App).use(Antd).mount('#app')
代碼以下:vuex
<template> <a-layout style="min-height: 100vh"> <a-layout-sider v-model:collapsed="collapsed" collapsible> <div class="logo" /> <a-menu theme="dark" v-model:selectedKeys="selectedKeys" mode="inline"> <a-menu-item key="1"> <pie-chart-outlined /> <span>Option 1</span> </a-menu-item> <a-menu-item key="2"> <desktop-outlined /> <span>Option 2</span> </a-menu-item> <a-sub-menu key="sub1"> <template #title> <span> <user-outlined /> <span>User</span> </span> </template> <a-menu-item key="3">Tom</a-menu-item> <a-menu-item key="4">Bill</a-menu-item> <a-menu-item key="5">Alex</a-menu-item> </a-sub-menu> <a-sub-menu key="sub2"> <template #title> <span> <team-outlined /> <span>Team</span> </span> </template> <a-menu-item key="6">Team 1</a-menu-item> <a-menu-item key="8">Team 2</a-menu-item> </a-sub-menu> <a-menu-item key="9"> <file-outlined /> <span>File</span> </a-menu-item> </a-menu> </a-layout-sider> <a-layout> <a-layout-header style="background: #fff; padding: 0" /> <a-layout-content style="margin: 0 16px"> <a-breadcrumb style="margin: 16px 0"> <a-breadcrumb-item>User</a-breadcrumb-item> <a-breadcrumb-item>Bill</a-breadcrumb-item> </a-breadcrumb> <div :style="{ padding: '24px', background: '#fff', minHeight: '360px' }"> Bill is a cat. </div> </a-layout-content> <a-layout-footer style="text-align: center"> Ant Design ©2018 Created by Ant UED </a-layout-footer> </a-layout> </a-layout> </template> <script> import { PieChartOutlined, DesktopOutlined, UserOutlined, TeamOutlined, FileOutlined, } from '@ant-design/icons-vue'; import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'Home', components: { PieChartOutlined, DesktopOutlined, UserOutlined, TeamOutlined, FileOutlined, }, data() { return { collapsed: ref(false), selectedKeys: ref(['1']), }; }, }); </script> <style> #components-layout-demo-side .logo { height: 32px; margin: 16px; background: rgba(255, 255, 255, 0.3); } .site-layout .site-layout-background { background: #fff; } [data-theme='dark'] .site-layout .site-layout-background { background: #141414; } </style>
代碼以下:
<template> <div class="login-container"> <a-form layout="horizontal" :model="formState" @finish="handleFinish" @finishFailed="handleFinishFailed" > <a-form-item> <a-input v-model:value="formState.user" placeholder="Username"> <template #prefix><UserOutlined style="color: rgba(0, 0, 0, 0.25)" /></template> </a-input> </a-form-item> <a-form-item> <a-input v-model:value="formState.password" type="password" placeholder="Password"> <template #prefix><LockOutlined style="color: rgba(0, 0, 0, 0.25)" /></template> </a-input> </a-form-item> <a-form-item> <a-button type="primary" html-type="submit" :disabled="formState.user === '' || formState.password === ''" > Log in </a-button> </a-form-item> </a-form> </div> </template> <script> import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'; import { defineComponent, reactive } from 'vue'; export default defineComponent({ name: 'Login', setup() { const formState = reactive({ user: '', password: '', }); const handleFinish = values => { console.log(values, formState); }; const handleFinishFailed = errors => { console.log(errors); }; return { formState, handleFinish, handleFinishFailed, }; }, components: { UserOutlined, LockOutlined, }, }); </script> <style> .login-container{ position: absolute; left: 0; right: 0; top: 0; bottom: 0; margin:auto; height: 600px; width: 500px; } </style>
代碼以下:
<template> <Home /> <!-- <Login /> --> </template> <script> import Home from './components/Home.vue' // import Login from './components/Login.vue' export default { name: 'App', components: { Home, // Login } } </script> <style> </style>
在終端執行命令:
yarn serve
vue-router 官方文檔 https://next.router.vuejs.org/zh/introduction.html
在終端執行命令:
yarn add vue-router@next
代碼以下:
import { createApp } from 'vue' import App from './App.vue' import Antd from 'ant-design-vue'; import { createRouter, createWebHashHistory } from 'vue-router'; import 'ant-design-vue/dist/antd.css'; import Home from './components/Home.vue' import Login from './components/Login.vue' const routes = [ { path: '/', component: Home }, { path: '/login', component: Login }, ] // 建立路由實例並傳遞 `routes` 配置 // 你能夠在這裏輸入更多的配置,但咱們在這裏 // 暫時保持簡單 const router = createRouter({ // 內部提供了 history 模式的實現。爲了簡單起見,咱們在這裏使用 hash 模式。 history: createWebHashHistory(), routes, // `routes: routes` 的縮寫 }) //建立並掛載根實例 //確保 _use_ 路由實例使 //整個應用支持路由。 createApp(App).use(Antd).use(router).mount('#app')
這裏的
是最頂層的出口!上面路徑匹配到的組件將會在此渲染
代碼以下:
<template> <router-view></router-view> </template> <script> export default { name: 'App', } </script> <style> </style>
由於咱們在 setup 裏面沒有訪問 this,因此咱們不能再直接訪問 this.$router 或 this.$route。做爲替代,咱們使用 useRouter 函數:
https://next.router.vuejs.org/zh/guide/advanced/composition-api.html
代碼以下:
<script> import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'; import { defineComponent, reactive } from 'vue'; import { useRouter } from 'vue-router' export default defineComponent({ setup() { const formState = reactive({ user: '', password: '', }); const router = useRouter() const handleFinish = values => { console.log(values, formState); router.push('/') }; const handleFinishFailed = errors => { console.log(errors); }; return { formState, handleFinish, handleFinishFailed, }; }, components: { UserOutlined, LockOutlined, }, }); </script>
在終端執行命令:
yarn serve
代碼以下:
import { createApp } from 'vue' import App from './App.vue' import Antd from 'ant-design-vue'; import { createRouter, createWebHashHistory } from 'vue-router'; import 'ant-design-vue/dist/antd.css'; import Home from './views/Home.vue' import Login from './views/Login.vue' const routes = [ { path: '/', component: Home }, { path: '/login', component: Login }, ] // 建立路由實例並傳遞 `routes` 配置 // 你能夠在這裏輸入更多的配置,但咱們在這裏 // 暫時保持簡單 const router = createRouter({ // 內部提供了 history 模式的實現。爲了簡單起見,咱們在這裏使用 hash 模式。 history: createWebHashHistory(), routes, // `routes: routes` 的縮寫 }) //建立並掛載根實例 //確保 _use_ 路由實例使 //整個應用支持路由。 createApp(App).use(Antd).use(router).mount('#app')
本文簡單的建立了兩個vue頁面,而且初試了vue-router,讓兩個簡單的頁面可以實現跳轉。在下一節的內容中將會進一步使用vue-router,更多vue-router內容能夠查看 https://next.router.vuejs.org/zh/introduction.html
https://github.com/Impartsoft/Simple_Vue/tree/main/simple-vue 1.antd
Ant Design of Vue 官方文檔 https://2x.antdv.com/docs/vue/getting-started-cn
vue-router 官方文檔 https://next.router.vuejs.org/zh/introduction.html