已發布·持續改進
Vue.js · Nuxt 指南 · 1/6
本章目前僅提供英文版。
This chapter walks through preparing Node.js, scaffolding a Vue project with create-vue and a Nuxt project with nuxi, and running the dev server. It also covers editor setup. Keep the relationship in mind: Vue is the UI framework, and Nuxt is a full-stack framework that adds routing conventions, server rendering and server APIs on top of it.
All Vue and Nuxt tooling runs on Node.js. Install the LTS release from nodejs.org and check the versions. npm ships with Node.js, but pnpm, Yarn and Bun work just as well.
node --version
npm --versionThe official scaffolding tool for Vue is create-vue. It generates a project that uses Vite as its build tool and asks a few questions along the way.
npm create vue@latestThe prompts look roughly like this. For a first project, TypeScript, Vue Router, Pinia and Vitest are good choices; everything else can be added later.
| Prompt | Meaning |
|---|---|
| Project name | Name of the folder to create |
| TypeScript | Use .ts files and script setup lang="ts" |
| Router | Add Vue Router for navigating between views |
| Pinia | Add the official store for shared state |
| Vitest | Add a unit testing setup |
| End-to-End Testing | Add a browser testing tool such as Playwright |
| ESLint and Prettier | Add linting and formatting |
Once the project exists, install the dependencies and start the dev server.
cd my-vue-app
npm install
npm run devOpen the address printed in the terminal (by default http://localhost:5173) to see the starter page. The Vite dev server supports hot module replacement (HMR): when you save a .vue file, the change appears without a full reload, and component state is usually preserved.
Open src/App.vue and replace its contents with the following. Anything declared inside <script setup> (variables, functions, imported components) is available directly in the template.
<script setup lang="ts">
import { ref } from "vue";
const name = ref("Vue");
const count = ref(0);
</script>
<template>
<h1>Hello, {{ name }}!</h1>
<input v-model="name" />
<button @click="count++">Clicked {{ count }} times</button>
</template>Typing in the input updates the heading, and clicking the button increments the number. You only changed data, and the page followed along: that is Vue's reactivity at work.
The names you pass to npm run are defined under scripts in package.json. A TypeScript project created with create-vue contains roughly the following, plus test:unit, test:e2e or lint depending on the features you selected.
{
"scripts": {
"dev": "vite",
"build": "run-p type-check \"build-only {@}\" --",
"preview": "vite preview",
"build-only": "vite build",
"type-check": "vue-tsc --build"
}
}dev starts the dev server, build runs type checking and the production build, and preview serves the build output locally. Script names vary between projects, so this section is a good first stop whenever you open an unfamiliar codebase.
If you know you need server rendering, file-based routing or server APIs, you can start with Nuxt right away. Nuxt's command-line tool, nuxi, creates the project; npm create nuxt@latest does the same thing.
npx nuxi@latest init my-nuxt-app
cd my-nuxt-app
npm run devThe Nuxt dev server listens on http://localhost:3000 by default. If you pick a package manager during setup, dependencies are installed for you. These nuxi commands come up often:
npx nuxi dev # start the dev server
npx nuxi build # production build
npx nuxi generate # generate a static site
npx nuxi module add pinia # add a module (Pinia here)In VS Code, install the official Vue - Official extension (formerly Volar). It provides syntax highlighting, completion and type checking inside templates for .vue files. In the browser, Vue DevTools lets you inspect the component tree, state and Pinia stores. In a create-vue project you can also run checks from the terminal:
npm run type-check # type-check .vue files with vue-tsc
npm run lint # if you selected ESLintnpm create vue@latest scaffolds a Vite-based Vue project; npm run dev starts it.npx nuxi@latest init scaffolds a Nuxt project whose dev server uses port 3000 by default.
0 則留言
登入 · 登入後即可留言。
來留下第一則留言吧。