Vue-v-if及switch的使用

本文详细介绍了Vue.js中常用的基础指令,包括v-for循环显示数组和对象数据、v-if条件渲染以及v-bind绑定属性等,并通过具体示例展示了这些指令的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先讲下v-for

<meta charset="utf-8">
		<!--1.引入vue.js-->
		<script src="js/vue.js"></script>
		<title> vue的实例</title>
	</head>
	<body>
		<!--2. 指定一个div层,用于绑定vue实例-->
		<div id="app">
			<!--v-pre:不编译模板语法,直接显示-->
			<p v-pre>{{msg}}</p>
			<hr />
			
			<!--显示所有数组中的数据-->
			<p v-for="item in items">{{item}}</p>
			
			<hr />
			<p v-for="val in values">{{val}}</p>
			<hr />
			
			<p v-for="(val,key) in values">{{key}}-{{val}}</p>
			<hr />
			
			<p v-for="(stu,index) in students">{{index}}-{{stu.score}}</p>
			<hr />
			
			<!--v-bind:动态把数据绑定到html属性中,属性不能用{{}}取值-->
			<select>
				<option v-for="obj in objects" v-bind:value="obj.value">{{obj.text}}</option>
			</select>
			<br/><br/><br/><br/><br/>
		</div>
	</body>
	<!--3. 创建vue实例-->
	<script>
		var vm=new Vue({
			el:"#app",
			data:{
				msg:100,
				items:[10,20,30,40,50], // 数组类型
				values:{ // json
					title: 'How to do lists in Vue',
					author: 'Jane Doe',
					publishedAt: '2016-04-10'
				},
				students:[ //数组+json
					{score:50},
					{score:80},
					{score:60},
					{score:90}
				],
				objects:{ // json+json
					city1:{text:'请选择',value:-1},
					city2:{text:'湖南',value:1},
					city3:{text:'北京',value:2},
				}
			}
		})
	</script>

在这里插入图片描述
可以正常打印
然后是v-if
很简单 看官方文档就能一下子理解,这里写个小案例

	<div class="box">
			<br/><br/><br/><br/>
			<button v-on:click="toggle($event)">显示切换</button>
			<!-- 默认true显示 false隐藏 -->
			<div v-if="toggleValue" class="test" style="width: 100px;height: 100px;background-color: #009E94;"></div>
			<p>结果为:{{toggleValue}}</p>
		</div>
		<script>
			const vm = new Vue({
				el:".box",
				data:{
						toggleValue:true
				},
				methods:{
					// 方法
					toggle(event){
						if(this.toggleValue==true){
							this.toggleValue = false;
						}
						else{
							this.toggleValue = true;
						}
					}
				}
			})
		</script>

最后是switch

<div class="box">
			<input type="text" v-model="num1" />
			<select v-model="type">
				<option v-for="num in nums" :value="num.value">{{num.text}}</option>
			</select>
			<input type="text" v-model="num2" />
			<button v-on:click="math($event)">计算</button>
			<p>结果为:{{sum}}</p>
		</div>
		<script>
			const vm = new Vue({
				el:".box",
				data:{
					nums:{
						type1:{text:'加法',value:0},
						type2:{text:'减法',value:1},
						type3:{text:'乘法',value:2},
						type4:{text:'除法',value:3}
						},
						num1:0,
						num2:0,
						type:0,
						sum:0
				},
				methods:{
					math(event){
						switch(this.type){
							case 0 :this.sum=parseInt(this.num1)+parseInt(this.num2); break;
							case 1 :this.sum=parseInt(this.num1)-parseInt(this.num2); break;
							case 2 :this.sum=parseInt(this.num1)*parseInt(this.num2); break;
							case 3 :this.sum=parseInt(this.num1)/parseInt(this.num2); break;
						}
					}
				}
			})
		</script>

共勉

### vue-awesome-swiper 集成 ECharts 的实现方法 在 Vue.js 项目中,`vue-awesome-swiper` 是一个非常流行的轮播组件库,而 `ECharts` 则是一个强大的数据可视化工具。为了将两者结合起来,在滑动页面上展示动态图表,可以按照以下方式操作。 #### 1. 安装依赖 首先需要安装必要的依赖包,包括 `swiper`, `vue-awesome-swiper` 和 `echarts`: ```bash npm install swiper vue-awesome-swiper echarts --save ``` 此命令会将所需的模块添加到项目的依赖项中[^2]。 --- #### 2. 创建 Swiper 组件并嵌入 ECharts 图表 通过创建一个新的 Vue 组件文件(如 `SwiperWithEcharts.vue`),可以在其中定义 Swiper 轮播结构以及每个 slide 上的 ECharts 实例化逻辑。 以下是完整的代码示例: ```html <template> <div class="swiper-container"> <!-- 初始化 Swiper --> <client-only> <swiper :options="swiperOption" ref="mySwiper"> <swiper-slide v-for="(chartData, index) in chartsData" :key="index"> <div :id="'chart' + index" style="width: 100%; height: 300px;"></div> </swiper-slide> <!-- 如果需要分页器 --> <div class="swiper-pagination" slot="pagination"></div> </swiper> </client-only> </div> </template> <script> import { Swiper, SwiperSlide } from 'vue-awesome-swiper'; import 'swiper/swiper-bundle.css'; // 引入样式 import * as echarts from 'echarts'; export default { components: { Swiper, SwiperSlide, }, data() { return { // Swiper 配置选项 swiperOption: { slidesPerView: 1, spaceBetween: 30, pagination: { el: '.swiper-pagination', clickable: true, }, observer: true, // 修改swiper自己或子元素时,自动初始化swiper observeParents: true, // 修改swiper的父元素时,自动初始化swiper }, // 每个 Slide 对应的数据 chartsData: [ { title: '柱状图', type: 'bar', data: [120, 200, 150, 80, 70] }, { title: '折线图', type: 'line', data: [220, 180, 90, 160, 140] }, { title: '饼图', type: 'pie', data: [{ name: 'A', value: 335 }, { name: 'B', value: 310 }] }, ], }; }, mounted() { this.initEcharts(); }, methods: { initEcharts() { this.chartsData.forEach((item, index) => { const chartDom = document.getElementById('chart' + index); if (chartDom) { const myChart = echarts.init(chartDom); let option; switch (item.type) { case 'bar': option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], }, yAxis: { type: 'value', }, series: [{ data: item.data, type: 'bar', }], }; break; case 'line': option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'], }, yAxis: { type: 'value', }, series: [{ data: item.data, type: 'line', }], }; break; case 'pie': option = { tooltip: { trigger: 'item', }, legend: { top: '5%', left: 'center', }, series: [ { name: 'Access From', type: 'pie', radius: ['40%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center', }, emphasis: { label: { show: true, fontSize: '18', fontWeight: 'bold', }, }, data: item.data, }, ], }; break; default: console.error(`Unsupported chart type ${item.type}`); } myChart.setOption(option); // 应用配置 } }); }, }, }; </script> <style scoped> .swiper-container { width: 100%; max-width: 600px; /* 可调整 */ } </style> ``` 上述代码实现了以下几个功能: - **Swiper 配置**:设置了基础参数,例如每屏显示的数量、间距和分页器[^4]。 - **ECharts 渲染**:针对不同的图表类型(柱状图、折线图、饼图)分别生成对应的实例,并绑定至 DOM 元素[^1]。 --- #### 3. 解决可能遇到的问题 如果发现某些情况下图表无法正常渲染,可能是由于以下原因导致: - **DOM 加载顺序问题**:确保在 `mounted()` 生命周期钩子中执行 ECharts 初始化逻辑。 - **响应式布局支持不足**:可以通过监听窗口大小变化事件重新触发 `resize` 方法解决[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值