Vue3+TypeScript+Router+Vuex+Ant-Design-Vue项目(四)—— vue-router 基本配置

App.vue

修改 App.vue 文件:

<script lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup
export default {
  name: 'App',
  setup() {
    // setup(props, context)
    // props - 组件接受到的属性 context - 上下文
    // vue3 中用 setup 函数整合了所有的 api;只执行一次,在生命周期函数前执行,所以在 setup 函数中拿不到当前实例 this,不能用 this 来调用 vue2 写法中定义的方法
  },
}
</script>

<template>
  <img alt="Vue logo" src="./assets/logo.png" />
  <router-view></router-view>
</template>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

router

src 下新建 router 文件夹,并在文件夹内创建 index.ts

import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: () => import("@/views/index.vue")
  },
  {
    path: '/example',
    name: 'example',
    component: ()=> import('@/views/example.vue')
  },
  {
    path: '/test',
    name: 'test',
    component: ()=> import('@/views/TestPage.vue')
  }
]

export default createRouter({
  // createWebHashHistory (hash路由 Hash模式 #)
  // createWebHistory (history路由 HTML5 模式 推荐,需要服务器配置支持)
  // createMemoryHistory 带缓存 history 路由
  history: createWebHistory(),
  routes
})

PS:在引入.vue文件时一定要带上后缀名,否则会报找不到文件。

main.ts

main.ts 修改:

import { createApp } from 'vue'
// 引入Antd 
import Antd from 'ant-design-vue'
import router from './router/index'
import App from './App.vue'
// 样式文件需要单独引入
import 'ant-design-vue/dist/antd.css'

const app = createApp(App)
app.use(Antd)
app.use(router)
app.mount('#app')

Views

index.vue

src 下新建 views文件夹,并在文件夹内创建 index.vue

<template>
  <HelloWorld></HelloWorld>
</template>

<script lang="ts">
import HelloWorld from "../components/HelloWorld.vue";
import { defineComponent } from "vue";
export default defineComponent({
  name: "Home",
  components: { HelloWorld },
  setup() {
    return {
    };
  },
});
</script>

HelloWorld.vue

components文件夹内创建 HelloWorld.vue

<template>
  <button @click="handleclick">点击跳转TestPage</button>
  <p>Edit <code>The HelloWorld.vue</code> to test hot module replacement.</p>
</template>

<script>
import { defineComponent } from "vue";
import { useRoute,useRouter } from 'vue-router'
export default defineComponent({
  name: 'Index',
  setup () {
    const route = useRoute() // 相当于 vue2 中的 this.$route route   用于获取当前路由数据
    const router = useRouter(); // 相当于 vue2 中的 this.$router router  用于路由跳转
    function handleclick () {
      router.push({ path: '/test' })
    }
    return {
      handleclick
    }
  }
})
</script>

TestPage.vue

views文件夹内创建 TestPage.vue

<template>
  <div class="about">
    <h1>Ant-Design-Vue</h1>
    <h3>Button</h3>
    <a-button type="primary"> Primary </a-button>
    <a-button>Default</a-button>
    <a-button type="dashed"> ashed </a-button>
    <a-button type="danger"> Danger </a-button>
    <a-config-provider :auto-insert-space-in-button="false">
      <a-button type="primary"> 按钮 </a-button>
    </a-config-provider>
    <a-button type="primary"> 按钮 </a-button>
    <a-button type="link"> Link </a-button>
    <h3>栅栏</h3>
    <a-row>
      <a-col :span="12"> col-12 </a-col>
      <a-col :span="12"> col-12 </a-col>
    </a-row>
    <a-row>
      <a-col :span="8"> col-8 </a-col>
      <a-col :span="8"> col-8 </a-col>
      <a-col :span="8"> col-8 </a-col>
    </a-row>
    <a-row>
      <a-col :span="6"> col-6 </a-col>
      <a-col :span="6"> col-6 </a-col>
      <a-col :span="6"> col-6 </a-col>
      <a-col :span="6"> col-6 </a-col>
    </a-row>
  </div>
</template>

页面效果:
在这里插入图片描述

2.x版本 和 3.x版本对比

2.x版本

import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('../views/Test.vue')
  }
]
const router = new VueRouter({
  mode:"history",
  routes
})
export default router

2.x 采用构造函数构建 Vue Router 实例。
在路由模式配置上,2.x 是配置mode option

3.x版本

import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('../views/Test.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})
export default router

3.x 采用 createRouter 去创建 Vue Router 实例。

在路由模式配置上,3.x是采用vue-router中的createWebHistory方法去创建history属性,history模式用createWebHistory方法创建history属性,hash模式则需要使用createWebHashHistory方法来创建。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 + TypeScript 是一种常用的前端开发技术组合,它结合了Vue框架的便捷性和TypeScript的类型检查能力,可以提高项目的可维护性和开发效率。下面是一些Vue3 + TypeScript项目经验的介绍: 1. 项目初始化:使用Vue CLI创建一个Vue3项目,并选择TypeScript作为项目的语言。这样可以自动生成一些基本配置和文件结构,方便快速开始开发。 2. 类型定义:在Vue3 + TypeScript项目中,可以使用TypeScript的类型系统来定义组件的props、data、computed等属性的类型,以及事件的参数类型等。这样可以在编码过程中及早发现潜在的类型错误,提高代码的健壮性。 3. 组件开发:在Vue3 + TypeScript项目中,可以使用装饰器(decorators)来定义组件选项,例如使用@Prop来定义props属性的类型,使用@Emit来定义事件的参数类型。这样可以更清晰地描述组件的接口和行为。 4. 路由管理:在Vue3 + TypeScript项目中,可以使用Vue Router进行路由管理。通过定义路由的类型和参数类型,可以在编码过程中获得更好的类型提示和错误检查。 5. 状态管理:在Vue3 + TypeScript项目中,可以使用Vuex进行状态管理。通过定义状态的类型和操作的参数类型,可以提高代码的可读性和可维护性。 6. 第三方库的使用:在Vue3 + TypeScript项目中,可以使用第三方库,并为其编写类型声明文件,以获得更好的类型检查和代码提示。例如,可以使用axios发送HTTP请求,并为其编写类型声明文件,以确保请求参数和响应数据的类型正确。 7. 单元测试:在Vue3 + TypeScript项目中,可以使用Jest等测试框架进行单元测试。通过编写类型安全的测试用例,可以提高代码的质量和可靠性。 8. 构建和部署:在Vue3 + TypeScript项目中,可以使用Webpack等构建工具进行项目的打包和优化。通过配置合适的TypeScript编译选项和Webpack插件,可以生成高效的生产环境代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值