当 vue 遇上 小程序 (3)

接 上一篇 [当 vue 遇上 小程序 (1)]

1.5、嵌套循环 

· Vue.js

<!-- App.vue -->
<template>
  <div id="app">    
     <div v-for="item in address">
        <p>{{item.province}}</p>
        <div>
            <span v-for="grandItem in item.cities">{{grandItem}}</span>
        </div>
    </div> 
  </div>
</template>
<script>
    export default {        
        data(){
            return{
                address: [
                  { province: "湖南", cities:["长沙", "株洲", "湘潭", "岳阳","衡阳"]},
                  { province: "广东", cities: ["广州", "深圳", "佛山", "珠海", "中山"]},
                  { province: "广西", cities: ["南宁", "柳州", "桂林", "玉林","北海"]}
                ]
            }
        }
    }
</script>

· 小程序

<!--A.wxml-->
<view>
   <view wx:for="{{address}}" wx:key="index">
        <text>{{item.province}}</text>
    	<view>
      		<text wx:for="{{item.cities}}" wx:for-item="grandItem" wx:key="index">{{grandItem}}</text>
    	</view>
  </view>
</view>
 
<!--A.js-->
Page({  
  data: {     
    address: [
      { province: "湖南", cities:["长沙", "株洲", "湘潭", "岳阳","衡阳"]},
      { province: "广东", cities: ["广州", "深圳", "佛山", "珠海", "中山"]},
      { province: "广西", cities: ["南宁", "柳州", "桂林", "玉林","北海"]}
    ]
  }
})

第二层也可以用 item 取值,但内外都是 item ,不利于使用和维护。

<text wx:for="{{item.cities}}" wx:key="index">{{item}}</text>

1.6、公用模板

其实公用模板,本身是单独的页面,在这里主要通过父页面传递参数,实现同页不同面!

1)、在 App.vue 调用 公共模块,传递一个或多个参数

除了 props ,传参还有 Vuex、emit、ref 等方法,具体可参考《vue 组件传值》

<template>
  <div id="app">   
    <Head :hd="[title,hd,articles]"></Head>
    <Article :atc="articles"></Article>
 </div>
</template>
<script>
  import Head from './components/sub/head';
  import Article from './components/sub/article';
  export default {
    name: 'app',   
    components:{
     Head,Article
    },
    data(){
      return{
        title:"标题",
        hd:{name:"标题",desc:"简述"},
        articles:[
          { name: "以梦为马", desc: "不负韶华" },
          { name: "面朝大海", desc: "不负韶华" }
        ]
    }
  }
</script>

· Vue.js

1)、公用模板:head.vue

接收多个参数组成的数组值

<template>
	<div class="head">
		<div>{{hd[0]}}</div>    	
		<div>
			<div>{{hd[1].name}}</div>
			<p>{{hd[1].desc}}</p>
		</div>    	
	</div>
</template>
<script>	
	export default{
		props:['hd']
	}
</script>

公用模板:article.vue

<template>
	<div class="article">
		<div v-for="(item,index) in atc" :key="index">
			<div>{{item.name}}</div>
			<div>{{item.desc}}</div>
		</div>  	
	</div>
</template>
<script>	
	export default{
		props:['atc']
	}
</script>

· 小程序

1)、在 index.wxml 调用 ,传递一个或多个参数

这里,is="head" ,也就是引用  name = "head" 的公用模板

<import src="../common/head.wxml" />
<import src="../common/article.wxml"/>
<view>
    <template is="head" data="{{title: '静态标题'}}"/>
    <template is="head" data="{{title}}"/> 
    <template is="head" data="{{hd,txt}}"/>
    <template is="{{articles.length>0?'article':'head'}}" data="{{articles}}"/>
</view>

index.js

Page({
  data: {    
    title:"标题",
    hd:{name:"标题",desc:"简述"},
    txt:"正文内容",
    articles:[
      { name: "以梦为马", desc: "不负韶华" },
      { name: "面朝大海", desc: "花开半夏" }
    ]
  }
})


 

2)、公用模板:head.wxml

<template name="head"> 
  <view class="page-head">
    <view class="page-head-title">{{title?title:hd.name}}</view>
    <view wx:if="{{hd}}" class="page-head-desc">{{hd.desc}}</view>
    <view wx:if="{{txt}}"">{{txt}}</view>
  </view>
</template>

公用模板:article.wxml

<template name="article">
    <view wx:for="{{articles}}" wx:key="index">
      <text>{{item.name}}</text>
      <view>{{item.desc}}</view>
    </view>  
</template>

*注意:给模块传递多个参数时,两者写法略有不同

Vue.js 

 <Head :hd="[title,hd,articles]"></Head>

小程序 

<template is="head" data="{{hd,cloud}}"/>

未完。。。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值