vue3+vuetify+Router的hello world

目标

在vue框架中,实验vuetify的材质特性

一、接入router

为简单起见,直接在main.js中引用

import { createApp } from 'vue'
import App from './App.vue'
import vuetify from './plugins/vuetify'
import {createRouter, createWebHashHistory} from 'vue-router'
import { loadFonts } from './plugins/webfontloader'

import Home from '@/views/Home.vue'
import About from '@/views/About.vue'

loadFonts()

const routes = [
  { path: '/', component: Home },
  { path: '/about', component: About },
]

const router = createRouter({
  // 4. Provide the history implementation to use. We are using the hash history for simplicity here.
  history: createWebHashHistory(),
  routes // short for `routes: routes`
})

createApp(App)
  .use(router)
  .use(vuetify)
  .mount('#app')

二、安装谷歌的Icon

npm install material-design-icons-iconfont -D

而后,在@plugins/vuetify.js中做如下设置:

// Styles
import '@mdi/font/css/materialdesignicons.css'
import 'material-design-icons-iconfont/dist/material-design-icons.css'
import 'vuetify/styles'

// Vuetify
import { createVuetify } from 'vuetify'
import { aliases, mdi } from 'vuetify/lib/iconsets/mdi'
import { md } from 'vuetify/lib/iconsets/md'

export default createVuetify({  
  icons:{
    defaultSet: 'mdi',
    aliases,
    sets: {
      md,
      mdi
    }
  }
}
)

三、编写主页面并测试材质

app.vue:

<template>
  <v-app>
    <v-main>
      <p class = "mb-1">
        <router-link to="/" class="pa-1"><v-icon icon="mdi-home"/><span>Go to Home</span></router-link> |
        <router-link to="/about" class="pa-1"><v-icon icon="md:question_mark" class="mr-1"/><span>About</span></router-link>
      </p>
      <v-divider thickness="2px" color="gray-darken-5" class="pb-1"></v-divider>
      <router-view></router-view>
    </v-main>
  </v-app>
</template>

<script>
//import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',

  components: {
    //HelloWorld,
  },

  data: () => ({
    //
  }),
}
</script>

Home.vue:

<template>
    <div class="bg-red-darken-2 text-left">
        <span class = "text-white ml-1">HOME PAGE</span>
    </div>
</template>

四、测试页面

npm run dev

打开页面,看到如图:
在这里插入图片描述

五、所涉及的属性查询

官方文档
颜色库
mdi-icon
google-icon

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 + TypeScript 中,使用 Vue Router 进行路由跳转可以按照以下步骤进行: 1. 安装 Vue Router:使用 npm 或 yarn 安装 Vue Router ```bash npm install vue-router # 或者 yarn add vue-router ``` 2. 创建路由实例:在你的 `main.ts` 文件中,创建一个路由实例并将其挂载到 Vue 实例上。 ```typescript import { createApp } from 'vue' import { createRouter, createWebHistory } from 'vue-router' import App from './App.vue' import Home from './views/Home.vue' import About from './views/About.vue' const routes = [ { path: '/', component: Home }, { path: '/about', component: About } ] const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes }) createApp(App).use(router).mount('#app') ``` 3. 在组件中使用路由跳转:在你的组件中使用 `<router-link>` 标签来生成链接,并使用 `$router.push` 方法来导航到其他页面。 ```html <template> <div> <h1>Hello, World!</h1> <router-link to="/">Home</router-link> <router-link to="/about">About</router-link> <button @click="goToHome()">Go to Home</button> <button @click="goToAbout()">Go to About</button> </div> </template> <script lang="ts"> import { defineComponent } from 'vue' import { RouteLocationNormalized, Router } from 'vue-router' export default defineComponent({ methods: { goToHome() { this.$router.push('/') // or this.$router.push({ path: '/' }) }, goToAbout() { this.$router.push('/about') // or this.$router.push({ path: '/about' }) } }, // 如果需要在组件中使用路由对象,可以按照以下方式定义组件实例类型 // 以便在组件中访问路由对象的类型检查和自动补全 beforeRouteEnter(to: RouteLocationNormalized, from: RouteLocationNormalized, next: (to?: any) => void) { next((vm: any) => { vm.$router // 路由对象 vm.$route // 当前路由信息对象 }) } }) </script> ``` 这样就完成了在 Vue 3 + TypeScript 中使用 Vue Router 进行路由跳转的基本步骤。需要注意的是,在使用路由对象时,需要定义组件实例的类型,以便在组件中访问路由对象时进行类型检查和自动补全。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值