props用于接收来自父组件的数据


具体应用

基本使用

  • App.vue
<template>
  <Test :url="url"/>
</template>

<script>
import Test from './components/test.vue'

export default {
  name: 'App',
  components: {
    Test
  },
  data: function () {
    return {
      url: './assets/home/theme6.jpg'
    }
  }
}
</script>
  • test.vue
export default {
  name: 'test',
  props: ['url'],
  mounted () {
    console.log('url:' + this.url)
  }
}

Vue - props属性



确定类型

  • props属性传值时需要注意使用v-bind指令确定类型
  • App.vue
<Test :url="url"
        :num="1"
        :flag="true"
        flag1 />
  • test.vue
export default {
  name: 'test',
  // props: ['url'],
  props: {
    url: {
      type: String,
      default: ''
    },
    num: {
      type: Number
    },
    flag: {
      type: Boolean
    },
    flag1: {
      type: Boolean
    }
  },
  mounted () {
    console.log('url: ' + this.url)
    console.log('num: ' + this.num)
    console.log('flag: ' + this.flag)
    console.log('flag1: ' + this.flag1) // 默认为true
  }
}
</script>

Vue - props属性



- End -
梦想是咸鱼
关注一下吧
Vue - props属性

相关文章:

  • 2022-12-23
  • 2021-05-23
  • 2022-01-22
  • 2021-05-30
  • 2022-12-23
  • 2021-09-11
  • 2022-02-23
  • 2021-07-17
猜你喜欢
  • 2021-09-21
  • 2021-11-05
  • 2022-12-23
  • 2021-08-09
  • 2022-12-23
  • 2021-11-15
相关资源
相似解决方案