vue create vue3-project
cd vue3-projectnpm install vue-router vuex// GlobalComponent.vue
<template> <div> <h1>Global Component</h1> </div>
</template>
<script>
export default { name: 'GlobalComponent'
}
</script>// 使用Button组件
<template> <el-button>Default Button</el-button>
</template>// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
const routes = [ { path: '/', name: 'Home', component: Home }, // ... 其他路由配置
]
const router = createRouter({ history: createWebHistory(process.env.BASE_URL), routes
})
export default router<template> <router-link to="/">Home</router-link>
</template>// store/index.js
import { createStore } from 'vuex'
export default createStore({ state() { return { count: 0 } }, mutations: { increment(state) { state.count++ } }, actions: { increment({ commit }) { commit('increment') } }
})// 使用Vuex在组件中增加计数
methods: { increment() { this.$store.dispatch('increment') }
}// 使用ref创建响应式数据
const count = ref(0)// 使用reactive创建响应式对象
const user = reactive({ name: 'John', age: 30
})// 创建计算属性
computed: { doubleCount() { return this.count * 2 }
}// 在模板中使用计算属性
<div>{{ doubleCount }}</div>// 创建侦听器
watch(user, (newValue, oldValue) => { console.log('User changed:', newValue)
})// 在侦听器中使用回调
watch(user.age, (newValue, oldValue) => { console.log('Age changed:', newValue)
})// 使用生命周期钩子
mounted() { console.log('Component mounted!')
}// 生命周期顺序
created() { console.log('Component created!')
}
mounted() { console.log('Component mounted!')
}// 创建自定义指令
const vHighlight = { mounted(el, binding) { el.style.backgroundColor = binding.value }
}// 在模板中使用自定义指令
<div v-highlight="'red'">Highlight me!</div>// 使用具名插槽
<template> <div> <slot name="header">Header</slot> <slot>Default content</slot> <slot name="footer">Footer</slot> </div>
</template>// 在父组件中使用插槽
<template> <CustomComponent> <template #header> <h1>Header</h1> </template> <template #footer> <p>Footer</p> </template> </CustomComponent>
</template>// 创建插件
export default { install(Vue) { Vue.prototype.$myPlugin = function() { console.log('My plugin is called!') } }
}// 使用插件
Vue.use(MyPlugin)// 创建待办事项列表
<template> <div> <input v-model="newTodo" @keyup.enter="addTodo" /> <ul> <li v-for="(todo, index) in todos" :key="index"> {{ todo }} <button @click="removeTodo(index)">Remove</button> </li> </ul> </div>
</template>
<script>
export default { data() { return { todos: [], newTodo: '' } }, methods: { addTodo() { if (this.newTodo.trim()) { this.todos.push(this.newTodo) this.newTodo = '' } }, removeTodo(index) { this.todos.splice(index, 1) } }
}
</script>// 使用TensorFlow.js
import * as tf from '@tensorflow/tfjs'
const model = tf.sequential()
model.add(tf.layers.dense({ units: 10, activation: 'relu', inputShape: [1] }))
model.add(tf.layers.dense({ units: 1 }))
model.compile({ optimizer: 'sgd', loss: 'meanSquaredError' })
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1])
const ys = tf.tensor2d([-2, -1, 0, 1, 2, 3], [6, 1])
model.fit(xs, ys, { epochs: 250 }).then(() => { console.log('Training finished!')
})// 使用PyTorch.js
import * as torch from 'torch'
const x = torch.tensor([1, 2, 3], requires_grad=True)
const y = x.pow(2)
y.backward(torch.tensor([1]))
console.log(x.grad)本文提供了50个Vue3实战代码示例,涵盖了Vue3的基础知识、组件、路由、状态管理、响应式、计算属性、侦听器、生命周期、自定义指令、插槽、插件、实战项目和与深度学习结合等内容。通过这些示例,你可以快速掌握Vue3,并在实际项目中应用所学知识。