漸進式 JavaScript 框架
Model
View
<!-開發環境版本,包含了有幫助的命令行警告->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-生產環境版本,優化了尺寸和速度->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
Use Declarative Rendering
<div id="app">
{{ message }}
</div>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
<!DOCTYPE html>
<html>
<head>
<title>My first Vue app</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
{{ message }}
</div>
<script>
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!'
}
})
</script>
</body>
</html>
app.message = "<your message>";
Vue-CLI 是 Vue 的命令行工具,可以快速開發大型單頁應用程式 (SPA)。
主要是提供開發者一個快速建置 Vue.js 架構的工具,
並基於 webpack,提供了許多相關的功能、套件安裝協助。
$ node -v
$ sudo npm install -g @vue/cli
$ npm install -g @vue/cli
$ vue
$ vue create <your-project-name>
Vue CLI v4.5.6
? Please pick a preset:
Default ([Vue 2] babel, eslint)
Default (Vue 3 Preview) ([Vue 3] babel, eslint)
❯ Manually select features
Vue CLI v4.5.6
? Please pick a preset: Manually select features
? Check the features needed for your project:
(Press <space> to select, <a> to toggle all,
<i> to invert selection)
❯◉ Choose Vue version
◉ Babel
◯ TypeScript
◯ Progressive Web App (PWA) Support
◯ Router
◯ Vuex
◯ CSS Pre-processors
◉ Linter / Formatter
◯ Unit Testing
◯ E2E Testing
Babel, Router, Vuex, Linter, Unit
Vue CLI v4.5.6
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, Linter, Unit
? Use history mode for router? (Requires proper server setup for index fallback in production) (Y/n)
使用 Router 歷史記錄模式
Vue CLI v4.5.6
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, Linter, Unit
? Use history mode for router? (Requires proper server setup for index fallback
in production) Yes
? Pick a linter / formatter config: (Use arrow keys)
❯ ESLint with error prevention only
ESLint + Airbnb config
ESLint + Standard config
ESLint + Prettier
選擇 ESLint + Prettier
Vue CLI v4.5.6
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, Linter, Unit
? Use history mode for router? (Requires proper server setup for index fallback
in production) Yes
? Pick a linter / formatter config: Prettier
? Pick additional lint features: (Press <space> to select, <a> to toggle all, <i
> to invert selection)
❯◉ Lint on save
◯ Lint and fix on commit
選擇 Lint on save
Vue CLI v4.5.6
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, Linter, Unit
? Use history mode for router? (Requires proper server setup for index fallback
in production) Yes
? Pick a linter / formatter config: Prettier
? Pick additional lint features: Lint on save
? Pick a unit testing solution:
Mocha + Chai
❯ Jest
選擇 Jest
Vue CLI v4.5.6
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, Linter, Unit
? Use history mode for router? (Requires proper server setup for index fallback
in production) Yes
? Pick a linter / formatter config: Prettier
? Pick additional lint features: Lint on save
? Pick a unit testing solution: Jest
? Where do you prefer placing config for Babel, ESLint, etc.? (Use arrow keys)
❯ In dedicated config files
In package.json
選擇 In dedicated config files
Vue CLI v4.5.6
? Please pick a preset: Manually select features
? Check the features needed for your project: Babel, Router, Vuex, Linter, Unit
? Use history mode for router? (Requires proper server setup for index fallback
in production) Yes
? Pick a linter / formatter config: Prettier
? Pick additional lint features: Lint on save
? Pick a unit testing solution: Jest
? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated confi
g files
? Save this as a preset for future projects? (y/N)
是否將上述配置儲存到 preset 的 default
DONE Compiled successfully in 18073ms
App running at:
- Local: http://localhost:8080/
- Network: http://192.168.0.11:8080/
Note that the development build is not optimized.
To create a production build, run npm run build.
運行專案項目
$ cd <your-project>
$ npm run serve
打開瀏覽器
我們開發只需要關注 src 的資料夾即可,其他的部分目前可以先放著沒關係。
├── README.md
├── babel.config.js
├── node_modules
│ ├──...
├── package-lock.json
├── package.json
├── public
│ ├── favicon.ico
│ └── index.html
└── src
├── App.vue
├── assets
│ └── logo.png
├── components
│ └── HelloWorld.vue
└── main.js
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App)
}).$mount('#app')
<template>
<div id="app">
...
</div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
...
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
...
}
</style>
每個 Vue 的應用程式都是從 Vue 建構式建立根實體開始,
再一個個元件(Components)搭建上去而來的,透過元件的方式能讓開發者將程式碼封裝而更好重用。
而每個 Component 當中的 data 都會是互相獨立的,所以資料傳遞時,通常會仰賴下面兩種做法:
使用 Vue.component(tagName, options) 來註冊一個元件。注意,元件的註冊必須在 Vue Instance 初始化前完成。
Vue.component('my-component-name', {
// ... options ...
})
<div id="app">
<prompt-component></prompt-component>
</div>
Vue.component('prompt-component', {
template: '<div><p>{{message}}</p><button @click="sayHi">Say Hi!</button></div>',
data: function () {
return {
message: 'Hello World!'
}
},
methods: {
sayHi: function() {
alert('Hi');
}
}
})
var vm = new Vue({
el: '#app',
});
var Prompt = {
template: '<div><p>{{ message }}</p><button @click="sayHi">Say Hi!</button></div>',
data: function () {
return {
message: 'Hello World!'
}
},
methods: {
sayHi: function() {
alert('Hi');
}
}
};
var vm = new Vue({
el: '#app',
components: {
'prompt-component': Prompt
}
});
apa 格式加載中