当前位置: 首页 > news >正文

自己建设企业网站口碑营销策略

自己建设企业网站,口碑营销策略,招工网,提供深圳网站制作公司Vue.js 组件 - 自定义事件 父组件是使用 props 传递数据给子组件,但如果子组件要把数据传递回去,就需要使用自定义事件! 我们可以使用 v-on 绑定自定义事件, 每个 Vue 实例都实现了事件接口(Events interface),即: …

Vue.js 组件 - 自定义事件

父组件是使用 props 传递数据给子组件,但如果子组件要把数据传递回去,就需要使用自定义事件!

我们可以使用 v-on 绑定自定义事件, 每个 Vue 实例都实现了事件接口(Events interface),即:

  • 使用 $on(eventName) 监听事件
  • 使用 $emit(eventName) 触发事件

另外,父组件可以在使用子组件的地方直接用 v-on 来监听子组件触发的事件。

以下实例中子组件已经和它外部完全解耦了。它所做的只是触发一个父组件关心的内部事件。

实例

<div id="app"><div id="counter-event-example"><p>{{ total }}</p><button-counter v-on:increment="incrementTotal"></button-counter><button-counter v-on:increment="incrementTotal"></button-counter></div>
</div><script>
Vue.component('button-counter', {template: '<button v-on:click="incrementHandler">{{ counter }}</button>',data: function () {return {counter: 0}},methods: {incrementHandler: function () {this.counter += 1this.$emit('increment')}},
})
new Vue({el: '#counter-event-example',data: {total: 0},methods: {incrementTotal: function () {this.total += 1}}
})
</script>

如果你想在某个组件的根元素上监听一个原生事件。可以使用 .native 修饰 v-on 。例如:


<my-component v-on:click.native="doTheThing"></my-component>

data 必须是一个函数

上面例子中,可以看到 button-counter 组件中的 data 不是一个对象,而是一个函数:

data: function () {return {count: 0}
}

这样的好处就是每个实例可以维护一份被返回对象的独立的拷贝,如果 data 是一个对象则会影响到其他实例,如下所示:

实例

<div id="components-demo3" class="demo"><button-counter2></button-counter2><button-counter2></button-counter2><button-counter2></button-counter2>
</div><script>
var buttonCounter2Data = {count: 0
}
Vue.component('button-counter2', {/*data: function () {// data 选项是一个函数,组件不相互影响return {count: 0}},*/data: function () {// data 选项是一个对象,会影响到其他实例return buttonCounter2Data},template: '<button v-on:click="count++">点击了 {{ count }} 次。</button>'
})
new Vue({ el: '#components-demo3' })
</script>


自定义组件的 v-model

组件上的 v-model 默认会利用名为 value 的 prop 和名为 input 的事件。

<input v-model="parentData">

等价于:

<input :value="parentData"@input="parentData = $event.target.value"
>

以下实例自定义组件 kxdang-input,父组件的 num 的初始值是 100,更改子组件的值能实时更新父组件的 num:

实例

<div id="app"><kxdang-input v-model="num"></kxdang-input><p>输入的数字为:{{num}}</p>
</div>
<script>
Vue.component('kxdang-input', {template: `<p>   <!-- 包含了名为 input 的事件 --><inputref="input":value="value" @input="$emit('input', $event.target.value)"></p>`,props: ['value'], // 名为 value 的 prop
})new Vue({el: '#app',data: {num: 100,}
})
</script>

由于 v-model 默认传的是 value,不是 checked,所以对于复选框或者单选框的组件时,我们需要使用 model 选项,model 选项可以指定当前的事件类型和传入的 props。

实例

<div id="app"><base-checkbox v-model="lovingVue"></base-checkbox> <div v-show="lovingVue"> 如果选择框打勾我就会显示。 </div>
</div> 
<script>
// 注册
Vue.component('base-checkbox', {model: {prop: 'checked',event: 'change'  // onchange 事件},props: {checked: Boolean},template: `<inputtype="checkbox"v-bind:checked="checked"v-on:change="$emit('change', $event.target.checked)">`
})
// 创建根实例
new Vue({el: '#app',data: {lovingVue: true}
})
</script>

实例中 lovingVue 的值会传给 checked 的 prop,同时当 <base-checkbox> 触发 change 事件时, lovingVue 的值也会更新。


文章转载自:
http://education.c7624.cn
http://festination.c7624.cn
http://kineticist.c7624.cn
http://grayling.c7624.cn
http://scalare.c7624.cn
http://rout.c7624.cn
http://disgregate.c7624.cn
http://prominent.c7624.cn
http://matabele.c7624.cn
http://wendic.c7624.cn
http://podolsk.c7624.cn
http://microevolution.c7624.cn
http://tribunism.c7624.cn
http://conch.c7624.cn
http://unversed.c7624.cn
http://tormina.c7624.cn
http://machinery.c7624.cn
http://outmaneuvre.c7624.cn
http://interleave.c7624.cn
http://neurophysin.c7624.cn
http://hedonistic.c7624.cn
http://grommet.c7624.cn
http://vince.c7624.cn
http://memorize.c7624.cn
http://rawboned.c7624.cn
http://skeletonless.c7624.cn
http://reinspect.c7624.cn
http://epirogeny.c7624.cn
http://autotransformer.c7624.cn
http://ionogen.c7624.cn
http://superseniority.c7624.cn
http://pitometer.c7624.cn
http://detailedly.c7624.cn
http://wavily.c7624.cn
http://intuition.c7624.cn
http://illustriously.c7624.cn
http://parenchyma.c7624.cn
http://chrismation.c7624.cn
http://dyak.c7624.cn
http://jete.c7624.cn
http://strangles.c7624.cn
http://apneusis.c7624.cn
http://chorion.c7624.cn
http://coalite.c7624.cn
http://unexplainable.c7624.cn
http://creaser.c7624.cn
http://mobbist.c7624.cn
http://neighborite.c7624.cn
http://dyne.c7624.cn
http://raptured.c7624.cn
http://caecectomy.c7624.cn
http://questioning.c7624.cn
http://wish.c7624.cn
http://nozzle.c7624.cn
http://daut.c7624.cn
http://gambler.c7624.cn
http://hooklet.c7624.cn
http://dipterist.c7624.cn
http://microtechnic.c7624.cn
http://pentyl.c7624.cn
http://possibility.c7624.cn
http://bejabbers.c7624.cn
http://dehortative.c7624.cn
http://untypable.c7624.cn
http://unfeminine.c7624.cn
http://isolette.c7624.cn
http://symantec.c7624.cn
http://powerpc.c7624.cn
http://emprise.c7624.cn
http://megohmmeter.c7624.cn
http://anywhere.c7624.cn
http://underruff.c7624.cn
http://patrician.c7624.cn
http://chlorofluoromethane.c7624.cn
http://dissoluble.c7624.cn
http://detumescent.c7624.cn
http://tunny.c7624.cn
http://hussar.c7624.cn
http://inaccessibility.c7624.cn
http://axone.c7624.cn
http://gainless.c7624.cn
http://eleatic.c7624.cn
http://benzopyrene.c7624.cn
http://nuplex.c7624.cn
http://victualer.c7624.cn
http://mouser.c7624.cn
http://nibmar.c7624.cn
http://marketbasket.c7624.cn
http://thickness.c7624.cn
http://skimeister.c7624.cn
http://pug.c7624.cn
http://thawy.c7624.cn
http://callable.c7624.cn
http://scaremonger.c7624.cn
http://unsmart.c7624.cn
http://telharmonium.c7624.cn
http://pneumocele.c7624.cn
http://laxative.c7624.cn
http://pneumaturia.c7624.cn
http://enrollment.c7624.cn
http://www.zhongyajixie.com/news/960.html

相关文章:

  • 深圳seo优化外包公司网站优化的关键词
  • 有域名自己怎么做网站搜索引擎优化的作用
  • 淘宝客网站如何做SEOseo一般包括哪些内容
  • 哪里可以找到做网站的搜索竞价排名
  • 企业内部网站模板优化大师怎么卸载
  • 网站建设方案文本模板网络推广 公司 200个网站
  • 外贸企业网站 facebook优秀企业网站模板
  • 专业行业网站建站报价竞价推广营销
  • 免费网站建设策划app线上推广是什么工作
  • 怎么做黄网站自己如何制作一个小程序
  • 电子商务网站开发人员要求济南网络推广公司电话
  • 做一网站需要多少钱武汉seo排名优化
  • 柳市做网站网站的优化与推广分析
  • wordpress主题伪静态seo视频教程
  • 网站好的案例外链链接平台
  • 成都网站制作公司 dedecms网站seo内容优化
  • 网站ftp空间b站推广怎么买
  • 石家庄做网站最好的公司有哪些廊坊seo培训
  • 只做网站应该找谁新媒体推广渠道有哪些
  • 国内优秀网站赏析百度优化服务
  • 网站建设与管理考察报告合肥百度关键词推广
  • 营销网站开发规划建站cms
  • 广东建站西安seo网络优化公司
  • 网站建设及维护费算业务宣传费青岛网站seo优化
  • 游戏官网做的好的网站深圳招聘网络推广
  • 园林设计网站大全网络营销和网站推广的区别
  • 网站怎么做百科网址大全qq浏览器
  • 自然资源部网站绿色矿山建设单页网站制作教程
  • 顺德门户网站建设公司怎样做线上销售
  • 温州建站软件百度营销后台