Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

vite + vue + ts

作者:续写丶未来式   发布日期:2026-06-12   浏览:103

// vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
});

// src/main.ts
import { createApp } from 'vue';
import App from './App.vue';

createApp(App).mount('#app');

// src/App.vue
<template>
  <div id="app">
    <h1>{{ message }}</h1>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  name: 'App',
  setup() {
    const message = ref('Hello Vite + Vue + TS!');
    return {
      message,
    };
  },
});
</script>

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

解释说明

  1. vite.config.ts:

    • 使用 defineConfig@vitejs/plugin-vue 来配置 Vite。
    • 设置了别名 @ 指向 src 文件夹,方便路径引用。
  2. src/main.ts:

    • 使用 createApp 创建 Vue 应用实例,并挂载到 HTML 的 #app 元素上。
  3. src/App.vue:

    • 使用 <template> 定义模板结构。
    • 使用 <script lang="ts"> 定义 TypeScript 代码,通过 defineComponentref 创建一个响应式的 message 变量。
    • 使用 <style scoped> 定义样式,scoped 属性确保样式仅应用于当前组件。

上一篇:vue getcurrentinstance

下一篇:vue3子组件调用父组件方法

大家都在看

vue.js devtools用法

three.js vue

vue js for循环

vue.min.js 本地引入

vue.js 3

highlight.js vue

vue.config.js 配置

vue.config.js 配置代理

vue.config.js devserv

vue.config.js transpi

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站