媒介
Vue.js 是一款漸進式JavaScript框架,它易於上手,功能富強,合適構建用戶界面。經由過程本文,你將懂得Vue.js的基本知識,並經由過程實戰案例進修怎樣將其利用於現實項目中。
第一部分:Vue.js基本知識
1. Vue.js簡介
Vue.js 是一個用於構建用戶界面的庫,它採用組件化的頭腦,容許開辟者將UI拆分紅獨破、可復用的組件。Vue.js 的核心庫只關注視圖層,易於上手,同時也可能與其余庫或已有項目集成。
2. 情況搭建
2.1 安裝Node.js情況
Vue.js 須要Node.js情況,因此起首須要安裝Node.js。
# 經由過程npm安裝Node.js
npm install -g nvm
nvm install node
2.2 安裝Vue CLI
Vue CLI 是一個官方命令行東西,用於疾速搭建Vue項目。
# 經由過程npm安裝Vue CLI
npm install -g @vue/cli
3. Vue組件基本
3.1 組件定義
Vue.js 利用<template>
, <script>
, <style>
三個標籤來定義組件。
<template>
<div>
<h1>{{ title }}</h1>
</div>
</template>
<script>
export default {
data() {
return {
title: 'Hello Vue!'
};
}
}
</script>
<style scoped>
h1 {
color: red;
}
</style>
3.2 組件註冊
在Vue實例中註冊組件。
new Vue({
el: '#app',
components: {
'my-component': MyComponent
}
});
4. 數據綁定
Vue.js 利用雙向數據綁定,將數據模型與視圖模型連接起來。
4.1 插值表達式
利用雙大年夜括弧{{ }}
停止數據綁定。
<div>{{ message }}</div>
4.2 屬性綁定
利用v-bind
指令停止屬性綁定。
<div v-bind:title="title">Hello Vue!</div>
4.3 變亂綁定
利用v-on
指令停止變亂綁定。
<button v-on:click="sayHello">Click me!</button>
5. 打算屬性跟偵聽器
5.1 打算屬性
打算屬性基於它們的依附停止緩存,只有在相幹依附產生改變時才會重新打算。
computed: {
reversedMessage() {
return this.message.split('').reverse().join('');
}
}
5.2 偵聽器
偵聽器容許開辟者履行非同步操縱。
watch: {
message(newVal, oldVal) {
console.log(`Message changed from ${oldVal} to ${newVal}`);
}
}
第二部分:Vue.js實戰案例
1. 實戰案例一:待服務項列表
1.1 項目構造
src/
|-- components/
| |-- TodoList.vue
| |-- TodoItem.vue
|-- App.vue
|-- main.js
1.2 TodoList組件
<template>
<div>
<input v-model="newTodo" @keyup.enter="addTodo" placeholder="Add a todo">
<ul>
<todo-item
v-for="(todo, index) in todos"
:key="todo.id"
:todo="todo"
@remove-todo="removeTodo(index)"
></todo-item>
</ul>
</div>
</template>
<script>
import TodoItem from './TodoItem.vue';
export default {
components: {
TodoItem
},
data() {
return {
newTodo: '',
todos: []
};
},
methods: {
addTodo() {
const todo = {
id: this.todos.length,
content: this.newTodo,
completed: false
};
this.todos.push(todo);
this.newTodo = '';
},
removeTodo(index) {
this.todos.splice(index, 1);
}
}
};
</script>
1.3 TodoItem組件
<template>
<div>
<span :class="{ completed: todo.completed }">{{ todo.content }}</span>
<button @click="$emit('remove-todo', index)">Remove</button>
</div>
</template>
<script>
export default {
props: {
todo: Object,
index: Number
}
};
</script>
<style scoped>
.completed {
text-decoration: line-through;
}
</style>
2. 實戰案例二:氣象利用
2.1 項目構造
src/
|-- components/
| |-- Weather.vue
|-- App.vue
|-- main.js
2.2 Weather組件
<template>
<div>
<input v-model="city" placeholder="Enter city name" @keyup.enter="fetchWeather">
<div v-if="weather">
<h1>Weather in {{ weather.name }}</h1>
<p>Temperature: {{ weather.main.temp }}°C</p>
<p>Weather: {{ weather.weather[0].description }}</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
city: '',
weather: null
};
},
methods: {
fetchWeather() {
const apiKey = 'YOUR_API_KEY';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${this.city}&appid=${apiKey}&units=metric`;
fetch(url)
.then(response => response.json())
.then(data => {
this.weather = data;
})
.catch(error => {
console.error('Error fetching weather:', error);
});
}
}
};
</script>
第三部分:總結
經由過程本文的進修,你曾經控制了Vue.js的基本知識跟實戰技能。經由過程以上案例,你可能進一步懂得Vue.js的組件化開辟、數據綁定、打算屬性、偵聽器等核心不雅點。在現實項目中,壹直現實跟積聚經驗,你將可能更好地應用Vue.js構建富強的用戶界面。