【Vue CLI 】(更新中)

目录

ref

用于在模板中为元素或子组件创建引用

javascript 复制代码
<template>
  <div>
    <input ref="myInput" type="text" placeholder="Enter your name">
  </div>
</template>

<script>
export default {
  mounted() {
    this.$refs.myInput.focus(); // 在 mounted 钩子中访问 ref
  }
};
</script>

props(用于从父组件向子组件传递数据)

javascript 复制代码
<script>
export default {
  props: {
    message: {
      type: String,
      required: true // 必须传递
    },
    count: {
      type: Number,
      default: 0 // 默认值
    }
  }
}
</script>

mixin

javascript 复制代码
<script>
import { requestMixin } from './requestMixin.js';

export default {
  mixins: [requestMixin],
  data() {
    return {
      items: []
    };
  },
  methods: {
    onFetched(data) {
      this.items = data;
    }
  },
  created() {
    this.$on('data-fetched', this.onFetched);
  }
};
</script>

插件(增强:Vue.use())

javascript 复制代码
Vue.use(plugins)

scoped(局部样式)

它允许你在组件内部定义样式,并且这些样式只作用于当前组件,不会影响其他组件。这有助于避免样式冲突,提高样式的可维护性和模块化。

css 复制代码
<style scoped  lang="css">

</style>

本地存储

自定义事件($emit)

用于从子组件向父组件传递消息。通过 $emit,子组件可以触发自定义事件,并传递数据给父组件

html 复制代码
<template>
	<div class="app">
 
		<!---通过父组件给子组件传递函数类型的props实现:子给父传递数据-->
		<School:getschoolName="getSchoolName"/>
		
		<!--通过父组件给了组件绑定一个自定义事件实现:了给父传递数据(第一种写法,使用@或v-on)-->
		 <Student @atguigu="getStudentName"/> 
		 
		<!--通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第二种写法,使用ref)-->
		<Student ref="student"/>
	</div>
</template> 
相关推荐
用户54277848515402 分钟前
Promise :从基础原理到高级实践
前端
用户4099322502125 分钟前
Vue3条件渲染中v-if系列指令如何合理使用与规避错误?
前端·ai编程·trae
Mr_Swilder8 分钟前
2025-12-20 vue3中 eslint9+和prettier配置
前端
code_YuJun10 分钟前
脚手架开发工具——判断文件是否存在 path-exists
前端
code_YuJun10 分钟前
脚手架开发工具——root-check
前端
用户542778485154011 分钟前
XMLHttpRequest、AJAX、Fetch 与 Axios
前端
打小就很皮...18 分钟前
React 实现富文本(使用篇&Next.js)
前端·react.js·富文本·next.js
智算菩萨1 小时前
实战:高级中文自然语言处理系统的Python设计与实现
前端·javascript·easyui
q_19132846951 小时前
基于SpringBoot2+Vue2的诗词文化传播平台
vue.js·spring boot·mysql·程序员·计算机毕业设计
远山无期1 小时前
解决Tailwind任意值滥用:规范化CSS开发体验
前端·css·eslint