查看“︁Node.js/nuxt/传值”︁的源代码
来自康健生活
←
Node.js/nuxt/传值
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 父传子 == eq: 使用 <code>:menus</code> 传递数据。 父组件 {{hc|pages/index.vue|<syntaxhighlight lang="html"> <template> <section class="main"> <ly-nav-menu :menus="LayoutNavMenu"></ly-nav-menu> ... <script> import LayoutNavMenu from "../components/index/NavMenu"; export default { components: { 'ly-nav-menu': LayoutNavMenu, }, data() { return { LayoutNavMenu: [], } }, asyncData({ params, query, $axios, error }) { return Promise.all([ $axios.get('/v2/body_parts/all'), ]).then(([ LayoutNavMenu]) => { return { LayoutNavMenu: LayoutNavMenu.data.list, ... } </script> </syntaxhighlight>}} 子组件 {{hc|../components/index/NavMenu.vue|<syntaxhighlight lang="html5"> <template> ... <template v-bind:index="item.id" v-for="item in Body_parts_all"> <el-submenu :index="item.id+''" :popper-append-to-body="false"> ... </template> <script> ... export default { name: 'ly-nav-menu', props: { menus: { type: Array, defaultValue: [] } }, ... computed: { Body_parts_all() { let BPA = this.menus.map((item, index) => { ... stopPropagation(e) { e = e || window.event if (e.stopPropagation) { //W3C阻止冒泡方法 e.stopPropagation() } else { e.cancelBubble = true //IE阻止冒泡方法 ... </syntaxhighlight>}} == 兄弟传值 == 现在有一个需求是路由跳转页面,然后刷新数据,但是路由地址不变,变的是路由地址后面 query 的参数。 <blockquote><pre> http://localhost:3000/search?pageView=SearchAll</pre></blockquote> === 子传父,父传子 === navmenu -> index -> list -> listleft <pre> 父亲:index.vue 儿子:list.vue list 的儿子,父亲的孙子:listLeft.vue 儿子:navmenu.vue</pre> {{Hc|index.vue|<syntaxhighlight lang="html5"> <NavMenu @searchPageViewValue="pageViewValue"/> <List :searchPageViewValue="searchPageViewValue"/> data () { return { searchPageViewValue: 'searchAll' } }, components: { NavMenu, List }, props: { pageView: { type: String, default: 'SearchAll' }, </syntaxhighlight>}} {{Hc|list.vue|<syntaxhighlight lang="html5"> <ListLeft :searchPageViewValue="searchPageViewValue"/> <listRight/> props: { searchPageViewValue: { type: String, default: 'searchAll' } }, </syntaxhighlight>}} {{Hc|listLeft.vue|<syntaxhighlight lang="html5"> <ListLeftArticle v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchQa'"/> <ListLeftFile v-if="searchPageType === 'SearchAll'"/> <ListLeftUser v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchUser'"/> <ListPagination/> </syntaxhighlight>}} {{Hc|navmenu.vue|<syntaxhighlight lang="html5"> <nuxt-link :key="$route.path+'?pageView=SearchAll'" :to="{path:'/search',query:{pageView:'SearchAll'}}" class="tags-li-title is-active" >综合 (24) </nuxt-link> watch: { $route (to, from) { this.$emit('searchPageViewValue', this.pageView) } }, </syntaxhighlight>}} === 路由 === 第二种方法是直接监听路由 {{Hc|listLeft.vue|<syntaxhighlight lang="html5"> <ListLeftArticle v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchQa'"/> <ListLeftFile v-if="searchPageType === 'SearchAll'"/> <ListLeftUser v-if="searchPageType === 'SearchAll' || searchPageType === 'SearchUser'"/> <ListPagination/> watch: { $route (to, from) { this.searchPageType = this.$route.query.pageView } }, </syntaxhighlight>}} == 事件车 == {{hc|eventBus.js|<syntaxhighlight lang="javascript"> import Vue from 'vue' export default new Vue(); </syntaxhighlight>}} {{hc|AllCountry.vue|<syntaxhighlight lang="javascript"> import EventBus from "@/router/eventBus"; mounted() { this.allCountryEvent(); }, methods: { renderData() { ... allCountryEvent() { EventBus.$emit('allCountry', { isCountry: this.isCountry, isDepartment: this.isDepartment, isCountryValue: this.isCountryValue, isDepartmentValue: this.isDepartmentValue, }) } </syntaxhighlight>}} {{hc|HospitalList.vue|<syntaxhighlight lang="javascript"> import EventBus from "@/router/eventBus"; methods: { ... allCountryMessage() { let _self = this; EventBus.$on('allCountry', (message) => { _self.allCountryCache = message; _self.currentPage = 1; _self.getData( message.isCountry, message.isCountryValue, message.isDepartment, message.isDepartmentValue, _self.currentPage ) }) } </syntaxhighlight>}}
此页面嵌入的页面:
模板:Hc
(
查看源代码
)
模板:Hc/style.css
(
查看源代码
)
返回
Node.js/nuxt/传值
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。