Veröffentlicht · wird verbessert
Vue.js · Nuxt-Anleitung · 2/6
Dieses Kapitel ist vorerst nur auf Englisch verfügbar.
Vue and Nuxt projects are organized differently. A create-vue project follows ordinary Vite conventions and you write the entry point yourself. In Nuxt, folder names carry meaning: put a file in the right place and it becomes a route, a component or a server API. This chapter tours both layouts and their main configuration files.
A project created with TypeScript, Router, Pinia and Vitest looks roughly like this:
my-vue-app/
├── index.html # Vite's entry HTML
├── public/ # static files copied as-is
├── src/
│ ├── main.ts # creates the app, registers plugins
│ ├── App.vue # root component
│ ├── assets/ # CSS and images processed by the build
│ ├── components/ # reusable components
│ ├── views/ # screen components mapped to routes
│ ├── router/index.ts # Vue Router setup
│ └── stores/ # Pinia stores
├── vite.config.ts
├── tsconfig.json
└── package.jsonSplitting views/ from components/ is a convention rather than a rule, but separating screens from reusable pieces keeps things easy to find as the project grows.
A Vue app is created with createApp, gets its plugins registered, and is mounted onto an element in index.html.
import { createApp } from "vue";
import { createPinia } from "pinia";
import App from "./App.vue";
import router from "./router";
import "./assets/main.css";
const app = createApp(App);
app.use(createPinia());
app.use(router);
app.mount("#app");Routes are declared explicitly in src/router/index.ts. Lazy-loading rarely visited views with () => import(...) keeps the initial bundle small.
import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{ path: "/", name: "home", component: HomeView },
{ path: "/about", name: "about", component: () => import("../views/AboutView.vue") },
],
});
export default router;The Vite config registers the Vue plugin and a path alias. @ points at src, so you can write import Foo from "@/components/Foo.vue".
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
plugins: [vue()],
resolve: {
alias: { "@": fileURLToPath(new URL("./src", import.meta.url)) },
},
});Environment variables go in .env files at the project root. Only names prefixed with are exposed to client code, as in .
VITE_import.meta.env.VITE_API_URLIn Nuxt, folder names are the conventions. Recent versions of Nuxt keep application code under an app/ directory and server code in server/ at the root. You will also see older projects with pages/ and components/ at the root.
my-nuxt-app/
├── app/
│ ├── app.vue # root component
│ ├── pages/ # file-based routes
│ ├── components/ # auto-imported components
│ ├── composables/ # auto-imported composables
│ ├── layouts/ # layouts that wrap pages
│ ├── middleware/ # route middleware
│ └── plugins/ # plugins run at app startup
├── server/
│ ├── api/ # /api/* endpoints
│ └── routes/ # server routes without the /api prefix
├── public/ # static files
└── nuxt.config.ts| Folder | Purpose |
|---|---|
pages/ | The file path becomes the URL; pages/about.vue serves /about |
components/ | Used directly in templates, no import needed |
composables/ | useXxx functions are imported automatically |
server/api/ | Each file becomes one API endpoint |
layouts/ | Shared frames such as headers and footers |
Nuxt configuration lives in a single defineNuxtConfig call: modules, global CSS, runtime config, per-route rendering rules and more.
export default defineNuxtConfig({
modules: ["@pinia/nuxt"],
css: ["~/assets/css/main.css"],
runtimeConfig: {
apiSecret: "", // server-only
public: { apiBase: "/api" } // also exposed to the client
},
devtools: { enabled: true },
});runtimeConfig values can be overridden at runtime with environment variables such as NUXT_API_SECRET and NUXT_PUBLIC_API_BASE, and are read in code with useRuntimeConfig(). Nuxt generates type information into the .nuxt/ folder, so keep that folder out of version control.
main.ts and declares routes explicitly in router/index.ts.@ alias in the Vite config and the VITE_ prefix for client-side environment variables.pages/, components/, composables/ and server/ are conventions, and configuration lives in nuxt.config.ts.runtimeConfig and inject them through environment variables.
0 Kommentare
Anmelden · Melde dich an, um einen Kommentar zu schreiben.
Schreib den ersten Kommentar.