Comments (1)
配置 base + router
vite.config.ts
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
// https://vitejs.dev/config/
export default ({ mode }) => {
const BASE = loadEnv(mode, process.cwd()).VITE_BASE_URL; //'/abc'
console.log("BASE :", BASE);
return defineConfig({
plugins: [vue()],
// process.env.VITE_BASE_URL import.meta.env.VITE_BASE_URL 在这里不可用
// process.env.VITE_BASE_URL 依赖 @types/node
base: BASE,
});
};
.env
VITE_BASE_URL=/abc
src/router.ts
import HelloWorld from "./components/HelloWorld.vue";
import About from "./components/About.vue";
import News from "./components/News.vue";
import * as VueRouter from "vue-router";
export const routes = [
{
path: "/",
component: HelloWorld,
redirect:'/home',
children: [],
},
{
path: "/about",
component: About,
},
{
path: "/news",
component: News,
},
];
console.log('import.meta.env:',import.meta.env)
console.log('import.meta.env.VITE_BASE_URL:',import.meta.env.VITE_BASE_URL)
export const router = VueRouter.createRouter({
// 4. 内部提供了 history 模式的实现。为了简单起见,我们在这里使用 hash 模式。
history: VueRouter.createWebHistory(import.meta.env.VITE_BASE_URL),
//history: VueRouter.createWebHistory('/abc'),
routes, // `routes: routes` 的缩写
});
src/main.ts
import { createApp } from "vue";
import App from "./App.vue";
import { router } from "./router";
const app = createApp(App);
app.use(router);
app.mount("#app");
src/env.d.ts
/// <reference types="vite/client" />
declare module '*.vue' {
import type { DefineComponent } from 'vue'
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
const component: DefineComponent<{}, {}, any>
export default component
}
interface ImportMetaEnv {
readonly VITE_BASE_URL:string
//readonly VITE_APP_TITLE: string
// 更多环境变量...
}
interface ImportMeta {
readonly env: ImportMetaEnv
}
src/App.vue
<script setup lang="ts">
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
import HelloWorld from './components/HelloWorld.vue'
</script>
<template>
<img alt="Vue logo" src="./assets/logo.png" />
<router-link to="/">Go to root</router-link>
<router-link to="/news">Go to news</router-link>
<router-link to="/about">Go to About</router-link>
<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>
src/components/About.vue
<script setup lang="ts">
import { ref } from 'vue'
defineProps<{ msg: string }>()
const count = ref(0)
</script>
<template>
<h1>'about'</h1>
</template>
<style scoped>
a {
color: #42b983;
}
label {
margin: 0 0.5em;
font-weight: bold;
}
code {
background-color: #eee;
padding: 2px 4px;
border-radius: 4px;
color: #304455;
}
</style>
src/components/HelloWorld.vue
<script setup lang="ts">
import { ref } from 'vue'
defineProps<{ msg: string }>()
const count = ref(0)
</script>
<template>
hello
<h1>{{ msg }}</h1>
</template>
<style scoped>
a {
color: #42b983;
}
label {
margin: 0 0.5em;
font-weight: bold;
}
code {
background-color: #eee;
padding: 2px 4px;
border-radius: 4px;
color: #304455;
}
</style>
src/components/News.vue
<script setup lang="ts">
import { ref } from 'vue'
defineProps<{ msg: string }>()
const count = ref(0)
</script>
<template>
<h1>'news'</h1>
</template>
<style scoped>
a {
color: #42b983;
}
label {
margin: 0 0.5em;
font-weight: bold;
}
code {
background-color: #eee;
padding: 2px 4px;
border-radius: 4px;
color: #304455;
}
</style>
from blog.
Related Issues (20)
- React Hooks HOT 4
- Visual Studio Code [VSCode] HOT 1
- React Router v6 HOT 1
- Vite HOT 2
- front business logic 前端业务逻辑 HOT 2
- preconstruct HOT 5
- 运维Ops HOT 2
- Turborepo
- Lint HOT 5
- Storybook HOT 3
- esbuild HOT 5
- Commissioning 调试 HOT 1
- express-gateway HOT 1
- Nestjs HOT 1
- 领域驱动设计 Domain-Driven Design DDD HOT 8
- yarn
- Arduino HOT 17
- C 语言 HOT 3
- IoT 物联网 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.