- 1. 我是父组件传的参数{{ msg }}
-
- <script>
- export default{
- // 1.接收父组件的值
- props:{
- msg:String
- }
- }
- script>
- // 1.1传给子组件值 <子组件 :msg="">子组件>
- <Son :msg="传值">Son>
- <script>
- // 引入子组件
- import Son from './Son.vue'
- export default {
- components: {
- // 1.1子组件
- Son
- }
- }
- script>
- // 2.2.@opParent必须和子组件一样
- <Son ref="SonRef" @opParent="parentMethod">Son>
- // 2.1.方法
- <button @click="opSon">button>
- // 引入子组件
- import Son from './Son.vue'
- export default {
- components: {
- // 子组件
- Son
- },
- methods: {
- opSon() {
- // 2.1.调用子组件方法
- tshi.$refs.sonRef.ParentOpSon("我是父组件传入的参数");
- },
- // 2.2.调用父组件方法
- parentMethod (e) {
- console.log('刷新')
- }
- }
- }
- 我是父组件传的参数{{ msg }}
-
-
- <script>
- export default{
- // 接收父组件的值
- props: {
- msg:String
- },
- methods: {
- // 2.1.子组件方法
- ParentOpSon (parentMsg) {
- console.log(parentMsg);
- }
- // 2.2.调用父组件方法
- opParent(){
- this.$emit("opParent","我是子组件调用父组件方法的参数");
- }
- }
- }
- script>
- {{ msg }}
- <script>
- import { ref, defineProps } from 'vue'
-
- const props = defineProps({
- msg: {
- type: String,
- default: ""
- }
- })
- script>
- <Son :msg="myMsg">Son>
- <script>
- import Son from './Son.vue'
- import { ref } from 'vue'
-
- const myMsg = ref("我是父组件传的值");
-
- script>
- <script>
- import { ref, defineProps, defineExpose } from 'vue'
-
- const props = defineProps({
- msg: {
- type: String,
- default: ""
- }
- })
-
- const msg2 = ref("");
-
- const ParentOpSon = (parentMsg) => {
- console.log("父组件的参数", parentMsg);
- }
-
- // 导出方法父组件才能用
- defineExpose({
- ParentOpSon
- })
-
- script>
-
-
- <div>
- <Son ref="sonRef">Son>
- <button @click="opSon">传给子组件方法button>
- div>
-
- <script>
- import Son from './Son.vue'
- import { ref } from 'vue'
-
- const sonRef = ref();
- const opSon = () => {
- sonRef.value.ParentOpSon("传给子组件的参数");
- }
-
- script>
- <div>
- <button @click="opParent">调用父组件方法button>
- div>
- <script>
-
- const emit = defineEmits();
-
- // 子组件调用父组件方法
- const opParent = () => {
- emit("opParent","调用父组件方法");
- }
-
-
-
- script>
-
- <div>
- <Son ref="sonRef" @opParent="parentMethod ">Son>
- <button @click="opSon">传给子组件方法button>
- div>
-
- <script>
- import Son from './Son.vue'
- import { ref } from 'vue'
-
- const parentMethod = (e) => {
- console.log(e);
- }
-
- script>