Vue(2)——脚手架与组件化开发

1. VueCLI

  • 安装npm i @vue/cli -g
  • 创建vue项目vue create 项目名称
    • 可以选择模板:vue3、vue2、手动选择功能
  • 项目目录结构
    • package.json:scripts中设置了一些命令
      • serve:打开一个本地的静态资源服务器 npm run serve
      • build:代码打包(打包的代码在dist目录下) npm run build
    • node_modules:第三方工具
    • public:不参与编译的资源
    • src:参与编译的资源
      • components(组件:包含了结构、样式和逻辑):保存自定义组件功能
      • App.vue:根组件
      • main.js:Vue应用的入口文件,对vue进行了基础配置
  • config:各种配置文件

2. 组件通信

2.1 父传子:通过props进行处理

//App.vue
<script setup>
import { ref } from 'vue'
import ChildComp from './ChildComp.vue'

const greeting = ref('Hello from parent')
</script>

<template>
  <ChildComp :msg="greeting" />
</template>
//ChildComp.vue
<script setup>
const props = defineProps({
  msg: String
})
</script>

<template>
  <h2>{{ msg || 'No props passed yet' }}</h2>
</template>

1
2

2.2 子传父:通过emits进行处理

//App.vue
<script setup>
import { ref } from 'vue'
import ChildComp from './ChildComp.vue'

const childMsg = ref('No child msg yet')
</script>

<template>
  <ChildComp @response="(msg) => childMsg = msg" />
  <p>{{ childMsg }}</p>
</template>
//ChildComp.vue
<script setup>
const emit = defineEmits(['response'])

emit('response', 'hello from child')
</script>

<template>
  <h2>Child component</h2>
</template>

1
2

3. 组件插槽

//App.vue
<script setup>
import { ref } from 'vue'
import ChildComp from './ChildComp.vue'

const msg = ref('from parent')
</script>

<template>
  <ChildComp>Message: {{ msg }}</ChildComp>
</template>
//ChildComp.vue
<template>
  <slot>Fallback content</slot>
</template>

1
2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值