VUEJS 2.0 sub-component access/call the instance of the parent component, vuejs2.0

Source: Internet
Author: User

VUEJS 2.0 sub-component access/call the instance of the parent component, vuejs2.0

Sometimes, due to layout problems, the child component needs to pass data to the parent component and execute a parent-level method, not to mention the Code:

Child components:

<Template> <div class = "isShowing" ref = "isShowing"> <div class = "menu-wrapper" ref = "scroll_warpper" v-show = "! Hid_show_switch "> <ul ref =" scroll_warpper_ul "> <li class =" menu-item "@ click =" goToFatherDetail (233) "> </li> </ul> </div> <v-loading class =" isloading "v-show =" hid_show_switch "> </v-loading> </div> </template> <script type = "text/ecmascript-6"> export default {methods: {goToFatherDetail (itemId) {// this. $ parent. $ router. push ('gotodetail'); console. log ('sub-component method follows '+ itemId); this. $ emit ('refreshbizlines' , ItemId);/* <span style = "font-family: Arial, Helvetica, sans-serif;"> itemId is the data to be transmitted by the sub-account.-It is very important here, refreshbizlines is the custom function monitored by the parent component $ on, not the custom function of the parent component. */</Span >}}; </script>

Parent component:

<Template> <div class = "main-wrapper"> <div class = "tab-wrapper"> <div class = "tab-item"> <router-link to =" /isShowing "class =" table-item-text "> loading </router-link> </div> <div class =" tab-item "> <router-link = "/willShow" class = "table-item-text"> coming soon </router-link> </div> <router-view class = "items-show" v-on: refreshbizlines = "goToDetail" keep-alive> </router-view> </div> </template> <script type = "text/ecmascript-6"> export default {methods: {goToDetail (itemId) {console. log ('parent component is yours: '+ itemId) ;}< strong >}; </script> </strong>

The parent component uses v-on as a monitoring function for detection. The final generated code is similar

on: {  "refreshbizlines": function($event) {  _vm.goToDetail(123)}}

Therefore, the principle is that the child component accesses the detection function refreshbizlines of the parent component. When the child component accesses the refreshbizlines, the function below refreshbizline is executed.

GoToDetail -- that is, the parent component's

GoToDetail Function

Note that

v-on:refreshbizlines="goToDetail"

It must be placed on the name of the module where your parent component calls the child component.

I wish you a pleasant coding experience.

The above VUEJS 2.0 sub-component access/call of the parent component instance is all the content shared by xiaobian. I hope to give you a reference, and I hope you can provide more support for the customer's house.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.