vue3路由基础用法

master
barney 2 years ago
parent f068148073
commit ac463184f9
  1. 1
      Vite-Vue-tutorial/src/services/products.js
  2. 14
      Vite-Vue-tutorial/src/utils/request.js
  3. 24
      vite-ts-project/.gitignore
  4. 3
      vite-ts-project/.vscode/extensions.json
  5. 16
      vite-ts-project/README.md
  6. 13
      vite-ts-project/index.html
  7. 1450
      vite-ts-project/package-lock.json
  8. 21
      vite-ts-project/package.json
  9. 1
      vite-ts-project/public/vite.svg
  10. 14
      vite-ts-project/src/App.vue
  11. 1
      vite-ts-project/src/assets/vue.svg
  12. 78
      vite-ts-project/src/main.ts
  13. 0
      vite-ts-project/src/style.css
  14. 3
      vite-ts-project/src/views/About.vue
  15. 12
      vite-ts-project/src/views/Detail.vue
  16. 3
      vite-ts-project/src/views/Home.vue
  17. 32
      vite-ts-project/src/views/List.vue
  18. 16
      vite-ts-project/src/views/Login.vue
  19. 8
      vite-ts-project/src/views/User.vue
  20. 3
      vite-ts-project/src/views/User/Info.vue
  21. 3
      vite-ts-project/src/views/User/Orders.vue
  22. 3
      vite-ts-project/src/views/User/Scores.vue
  23. 7
      vite-ts-project/src/vite-env.d.ts
  24. 18
      vite-ts-project/tsconfig.json
  25. 9
      vite-ts-project/tsconfig.node.json
  26. 7
      vite-ts-project/vite.config.ts

@ -10,4 +10,5 @@ import { get } from '../utils/request'
export const loadProductAPI = (page = 1, category = '') => get('/api/v1/products/', {
page,
category,
// per, 每页最多多少数据
});

@ -5,16 +5,17 @@ import "nprogress/nprogress.css";
export const serverUrl = "http://localhost:1337";
const instance = axios.create({
baseURL: serverUrl, // 请求的基础地址
timeout: 5000, // 超时时间
baseURL: serverUrl, // baseURL会在发送请求的时候拼接在url参数的前面
timeout: 5000, // 设置超时时间
});
/**
* get 请求
* @param {*} url 请求地址
* @param {*} params 传递的数据
* @param {*} params url中传递的数据,拼接在url后面
* @returns
*/
// axios.get(url, config)请求第二个参数表示配置信息(包括请求数据、请求头等)
export const get = (url, params) =>
instance.get(url, {
params: params, // url传递的参数
@ -26,6 +27,7 @@ export const get = (url, params) =>
* @param {*} data 数据
* @returns
*/
// axios.post(url, data, config)请求第三个参数表示配置信息(包括请求数据、请求头等)
export const post = (url, data) => instance.post(url, data);
/**
@ -46,6 +48,9 @@ export const del = (url) => instance.delete(url);
// Add a request interceptor (全局请求拦截)
instance.interceptors.request.use(
function (config) {
// console.group("全局请求拦截");
// console.log(config);
// console.groupEnd();
NProgress.start(); // 启动进度条
// 这里还可以设置token
return config;
@ -58,6 +63,9 @@ instance.interceptors.request.use(
// Add a response interceptor (全局响应拦截)
instance.interceptors.response.use(
function (response) {
// console.group("全局响应拦截");
// console.log(response);
// console.groupEnd();
NProgress.done(); // 关闭进度条
return response.data;
},

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

@ -0,0 +1,3 @@
{
"recommendations": ["Vue.volar"]
}

@ -0,0 +1,16 @@
# Vue 3 + TypeScript + Vite
This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
## Recommended IDE Setup
- [VS Code](https://code.visualstudio.com/) + [Volar](https://marketplace.visualstudio.com/items?itemName=Vue.volar)
## Type Support For `.vue` Imports in TS
Since TypeScript cannot handle type information for `.vue` imports, they are shimmed to be a generic Vue component type by default. In most cases this is fine if you don't really care about component prop types outside of templates. However, if you wish to get actual prop types in `.vue` imports (for example to get props validation when using manual `h(...)` calls), you can enable Volar's Take Over mode by following these steps:
1. Run `Extensions: Show Built-in Extensions` from VS Code's command palette, look for `TypeScript and JavaScript Language Features`, then right click and select `Disable (Workspace)`. By default, Take Over mode will enable itself if the default TypeScript extension is disabled.
2. Reload the VS Code window by running `Developer: Reload Window` from the command palette.
You can learn more about Take Over mode [here](https://github.com/johnsoncodehk/volar/discussions/471).

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite-TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

File diff suppressed because it is too large Load Diff

@ -0,0 +1,21 @@
{
"name": "vite-ts-project",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc --noEmit && vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.2.37",
"vue-router": "^4.1.5"
},
"devDependencies": {
"@vitejs/plugin-vue": "^3.1.0",
"typescript": "^4.6.4",
"vite": "^3.1.0",
"vue-tsc": "^0.40.4"
}
}

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

@ -0,0 +1,14 @@
<script setup lang="ts">
</script>
<template>
<router-link :to="{name: 'Home'}">首页</router-link>
<router-link :to="{name: 'List'}">列表</router-link>
<router-link :to="{name: 'User'}">我的</router-link>
<router-link :to="{name: 'About'}">关于</router-link>
<hr/>
<router-view></router-view>
</template>
<style scoped>
</style>

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>

After

Width:  |  Height:  |  Size: 496 B

@ -0,0 +1,78 @@
import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import './style.css'
import App from './App.vue'
const router = createRouter({
history: createWebHistory(),
routes: [
{
path: '/',
name: 'Home',
component: () => import ('./views/Home.vue'),
},
{
path: '/detail',
name: 'Detail',
component: () => import ('./views/Detail.vue'),
},
{
path: '/about',
name: 'About',
component: () => import ('./views/About.vue'),
},
{
path: '/list',
name: 'List',
component: () => import ('./views/List.vue'),
},
{
path: '/login',
name: 'Login',
component: () => import ('./views/Login.vue'),
},
{
path: '/user',
name: 'User',
component: () => import ('./views/User.vue'),
meta: {
needLogin: true, // 该页面需要登录
},
children: [ // 嵌套路由
{
path: 'info',
name: 'userInfo',
component: import('./views/User/Info.vue'),
},
{
path: 'scores',
name: 'userScores',
component: import('./views/User/Scores.vue'),
},
{
path: 'orders',
name: 'userOrders',
component: import('./views/User/Orders.vue'),
},
]
},
]
})
router.beforeEach((to,from,next) => {
// console.log(to.query);
// 这里可以做路由拦截
// 判断当前页面是否需要登录
if (to.meta.needLogin) {
if (localStorage.getItem('token')) { // 有token不需要登陆
next();
}else {
next({
name: 'Login',
})
}
}else {
next();
}
})
createApp(App).use(router).mount('#app')

@ -0,0 +1,3 @@
<template>
<h1>关于</h1>
</template>

@ -0,0 +1,12 @@
<template>
<h1>详情</h1>
<hr />
<div>查询条件为{{query}}</div>
</template>
<script setup lang="ts">
import { useRoute } from 'vue-router';
const { query } = useRoute();
</script>

@ -0,0 +1,3 @@
<template>
<h1>首页</h1>
</template>

@ -0,0 +1,32 @@
<template>
<h1>列表</h1>
<hr />
<div class="list">
<router-link
:to="{ name: 'Detail', query: { id: item.id } }"
v-for="item in courses"
:key="item.id"
><li>{{ item.name }}</li></router-link
>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
const courses = ref([
{
id: 1,
name: "语文",
},
{
id: 2,
name: "数学",
},
{
id: 3,
name: "英语",
},
]);
</script>

@ -0,0 +1,16 @@
<template>
<h1>登录页</h1>
<button @click="login">点击登录</button>
</template>
<script setup lang="ts">
import { useRouter } from 'vue-router';
const { replace } = useRouter();
const login = () => {
localStorage.setItem("token", "1111");
replace({
name: 'User',
})
};
</script>

@ -0,0 +1,8 @@
<template>
<h1>用户页</h1>
<router-link :to="{name: 'userInfo'}">个人信息</router-link>
<router-link :to="{name: 'userScores'}">我的订单</router-link>
<router-link :to="{name: 'userOrders'}">我的积分</router-link>
<hr />
<router-view></router-view>
</template>

@ -0,0 +1,3 @@
<template>
<h3>个人信息</h3>
</template>

@ -0,0 +1,3 @@
<template>
<h3>我的订单</h3>
</template>

@ -0,0 +1,3 @@
<template>
<h3>我的积分</h3>
</template>

@ -0,0 +1,7 @@
/// <reference types="vite/client" />
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}

@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
"jsx": "preserve",
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}

@ -0,0 +1,9 @@
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}

@ -0,0 +1,7 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()]
})
Loading…
Cancel
Save