从0搭建一个最最最基础的vue3+ts项目(包含element plus引入,路由跳转)

1.创建vue3+TS项目的命令:

npm create vite@latest xxx -- --template vanilla-ts

创建完成后,在vscode中打开项目,npm i 下包,然后npm run dev运行项目,看到以下页面代表vue3+ts项目创建成功!

2.创建完之后下载element plus:

npm install element-plus --save

再下载icon图标:

npm install @element-plus/icons-vue

同时在main.ts中编写如下代码(可直接复制下方代码覆盖原来的代码)

import { createApp } from 'vue'
import './style.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(ElementPlus)
app.mount('#app')

此时,你就可以在你的页面中使用element plus的内容了!

3.下载路由:

npm i vue-router@4

在main.ts中,引入router并使用router:

import router from "./router",

app.use(router)

main.ts的完整代码如下:

import { createApp } from 'vue'
import './style.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import router from "./router"

const app = createApp(App)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(ElementPlus)
app.use(router)
app.mount('#app')

此时,你就可以正常使用路由跳转了!

4.路由跳转示例

以下内容包括除了初始文件外的所以代码~
文件布局如下:

// router.ts
import { createRouter, createWebHashHistory } from "vue-router"

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            name: 'home',
            component: () => import('./components/home.vue')
        },

        {
            path: "/about",
            name: 'about',
            component: () => import('./components/about.vue')
        },
        {
            path: "/HelloWorld",
            name: 'HelloWorld',
            component: () => import('./components/HelloWorld.vue')
        },
        {
            path: "/small",
            name: 'small',
            component: () => import('./components/small.vue')
        },
    ]
});


export default router;
//main.ts
import { createApp } from 'vue'
import './style.css'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import router from "./router"

const app = createApp(App)

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(ElementPlus)
app.use(router)
app.mount('#app')
//App.vue
<script setup lang="ts">

</script>

<template>
  <router-view></router-view>
</template>

<style scoped></style>
//home.vue
<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import router from "../router"; //用到router-link
import { useRoute } from "vue-router";

const isshow = ref(true); //v-if默认按钮要展示
const currentRoute = ref(useRoute());
// 监听浏览器上方的路由地址!
watch(
  () => currentRoute.value.fullPath,
  (newRoute, oldRoute) => {
    // console.log("路由路径:", newRoute);
    if (newRoute !== "/") {
      isshow.value = false;
    } else {
      isshow.value = true;
    }
  }
);
</script>

<template>
  <button v-if="isshow">
    <!-- 第一种跳转路由的方法 router-link -->
    <router-link to="/HelloWorld">去HelloWorld</router-link>
  </button>
  <button v-if="isshow">
    <!-- 第一种跳转路由的方法 router-link -->
    <router-link to="/about">去about</router-link>
  </button>
</template>

<style scoped></style>
//small.vue
<template>
  <h3>small</h3>
  <h3>small</h3>
  <h3>small</h3>
</template>

<script>
 
</script>

<style>

</style>
//HelloWorld.vue
<script setup lang="ts">
import router from "../router";
const submitForm = () => {
  // 第二种跳转路由的方法
  router.push({ path: "/small" });
};
</script>

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

  <div>
    <!-- 第二种跳转路由的方法 -->
    <el-button @click="submitForm">跳转到small</el-button>
  </div>
</template>

<style scoped></style>
//about.vue
<template>
  <h3>about</h3>
  <h3>about</h3>
  <h3>about</h3>
</template>

<script>
 
</script>

<style>

</style>

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搭建一个vue3 ts pinia element-plus后台管理系统框架,可以按照以下步骤进行: 1. 安装Node.js和Vue CLI Vue CLI是用于创建和管理Vue项目的官方工具,需要先安装Node.js和Vue CLI。 2. 创建基于Vue3和TypeScript项目 使用Vue CLI创建一个基于Vue3和TypeScript项目,输入以下命令: ``` vue create my-project --preset=@vue/cli-plugin-typescript ``` 3. 安装Pinia Pinia是一个Vue 3状态管理库,可以用于管理应用程序的状态。在项目中安装Pinia,输入以下命令: ``` npm install pinia ``` 4. 安装Element Plus Element Plus是一个基于Vue.js 3的UI库,可用于构建后台管理系统。在项目中安装Element Plus,输入以下命令: ``` npm install element-plus ``` 5. 创建页面和组件 根据项目需求,创建页面和组件。 6. 配置Pinia 在main.ts引入Pinia,并在创建Vue实例时使用它。示例代码如下: ```typescript import { createApp } from 'vue' import App from './App.vue' import { createPinia } from 'pinia' const app = createApp(App) app.use(createPinia()) app.mount('#app') ``` 7. 配置Element Plus 在main.ts引入Element Plus,并在创建Vue实例时使用它。示例代码如下: ```typescript import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' const app = createApp(App) app.use(ElementPlus) app.mount('#app') ``` 8. 编写页面和组件 使用Vue3和TypeScript编写页面和组件。 9. 运行项目 运行项目,输入以下命令: ``` npm run serve ``` 以上就是搭建一个vue3 ts pinia element-plus后台管理系统框架的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值