1. 首页 > IT综合教程 > 正文

it教程FG124-Vue.js基础

内容大纲

1. Vue.js简介

Vue.js是一个渐进式的JavaScript框架,用于构建用户界面。它采用组件化开发方式,具有响应式数据绑定和组合式API等特性,使得前端开发更加简单和高效。Vue.js的主要特点包括:

  • 渐进式框架:可以逐步集成到项目中,从简单的表单处理到复杂的单页应用
  • 响应式数据绑定:自动追踪数据变化并更新DOM
  • 组件化开发:将UI拆分为独立的、可复用的组件
  • 模板语法:使用简洁的HTML-based模板语法
  • 组合式API:在Vue 3中引入,提供更灵活的代码组织方式
  • 性能优化:虚拟DOM、编译时优化等技术提高渲染性能

Vue.js由尤雨溪创建,是当前最流行的前端框架之一,广泛应用于各种Web应用开发。更多学习教程www.fgedu.net.cn

2. Vue.js安装

2.1 使用CDN引入





2.2 使用npm安装

$

# 安装Vue 3

$

npm install vue@latest

2.3 使用Vite创建Vue项目

$

# 使用npm create vite@latest创建Vue项目

$

npm create vite@latest my-vue-app — –template vue

$

# 进入项目目录

$

cd my-vue-app

$

# 安装依赖

$

npm install

$

# 启动开发服务器

$

npm run dev

3. Vue.js基本概念

3.1 响应式数据绑定

// Vue 3组合式API
import { createApp, ref } from 'vue'

createApp({
  setup() {
    const count = ref(0)
    
    return {
      count
    }
  }
}).mount('#app')

3.2 模板语法


{{ message }}
显示内容
{{ item.name }}
{{ computedProperty }}
{{ watchedProperty }}

3.3 计算属性和监听器

// 计算属性
const computedProperty = computed(() => {
  return count.value * 2
})

// 监听器
watch(count, (newValue, oldValue) => {
  console.log(`Count changed from ${oldValue} to ${newValue}`)
})

4. Vue.js组件

4.1 组件定义

// 定义组件
const HelloWorld = {
  props: {
    msg: String
  },
  setup(props) {
    return {
      props
    }
  },
  template: `
    

{{ props.msg }}

` } // 使用组件 createApp({ components: { HelloWorld } }).mount('#app')

4.2 组件通信

4.2.1 父组件向子组件传递数据(props)

// 父组件


// 子组件
const ChildComponent = {
  props: {
    message: String
  },
  setup(props) {
    return {
      props
    }
  }
}

4.2.2 子组件向父组件传递数据(emit)

// 子组件
const ChildComponent = {
  emits: ['update'],
  setup(props, { emit }) {
    const handleClick = () => {
      emit('update', 'Hello from child')
    }
    
    return {
      handleClick
    }
  }
}

// 父组件


5. Vue.js状态管理

5.1 组件内状态

import { ref, reactive } from 'vue'

// 使用ref定义响应式数据
const count = ref(0)

// 使用reactive定义响应式对象
const user = reactive({
  name: 'John',
  age: 30
})

// 修改数据
count.value++
user.age = 31

5.2 全局状态管理(Pinia)

$

# 安装Pinia

$

npm install pinia

// 创建store
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 0
  }),
  getters: {
    doubleCount: (state) => state.count * 2
  },
  actions: {
    increment() {
      this.count++
    }
  }
})

// 使用store
import { useCounterStore } from './stores/counter'

const store = useCounterStore()
store.increment()
console.log(store.count)
// 1
console.log(store.doubleCount)
// 2

6. Vue.js生命周期

6.1 组合式API生命周期钩子

import { onMounted, onUpdated, onUnmounted } from 'vue'

setup() {
  // 组件挂载后调用
  onMounted(() => {
    console.log('Component mounted')
  })
  
  // 组件更新后调用
  onUpdated(() => {
    console.log('Component updated')
  })
  
  // 组件卸载前调用
  onUnmounted(() => {
    console.log('Component unmounted')
  })
}

6.2 生命周期顺序

  1. 创建阶段:setup → onBeforeMount → onMounted
  2. 更新阶段:onBeforeUpdate → onUpdated
  3. 卸载阶段:onBeforeUnmount → onUnmounted

7. Vue Router

7.1 安装和配置

$

# 安装Vue Router

$

npm install vue-router@4

// 创建路由配置
import { createRouter, createWebHistory } from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    component: About
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 应用路由
import { createApp } from 'vue'
import App from './App.vue'

const app = createApp(App)
app.use(router)
app.mount('#app')

7.2 路由导航


首页
关于我们


import { useRouter } from 'vue-router'

const router = useRouter()

const goToHome = () => {
  router.push('/')
}

const goToAbout = () => {
  router.push({ name: 'About' })
}

8. Vue.js最佳实践

生产环境建议

  • 使用组合式API(Composition API)组织代码
  • 合理使用ref和reactive管理状态
  • 使用计算属性处理复杂逻辑,提高性能
  • 使用Pinia进行全局状态管理
  • 组件拆分要合理,保持组件的单一职责
  • 使用Vue Router进行路由管理
  • 使用ESLint和Prettier保持代码质量
  • 编写单元测试和集成测试
  • 使用TypeScript提高代码可维护性
  • 优化图片和资源加载
  • 使用代码分割减少初始加载时间
  • 合理使用v-memo、v-once等指令优化性能

风哥风哥提示:Vue.js的生态系统非常丰富,建议学习Vuex(状态管理)、Vue Router(路由)、Pinia(新一代状态管理)等相关库,以构建更复杂的应用。

学习交流加群风哥微信: itpux-com

学习交流加群风哥QQ113257174

更多学习教程公众号风哥教程itpux_com

author:www.itpux.com

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息