You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.2 KiB
Vue

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<el-card header="当一个vue页面有多个相同dom结构时dom比较简单不想提一个子组件这个时候可以使用jsx语法">
<h3>vue template写法</h3>
<ul class="demo-jsx-ul">
<li
v-for="item in list"
:key="item.name"
>
<div>姓名: {{ item.name }}</div>
<div>年龄: {{ item.age }}</div>
<div>地址: {{ item.address }}</div>
</li>
</ul>
<h3>vue jsx写法DOM一</h3>
<renderJsx
refName="JsxUlRef1"
label="1"
:list="list"
/>
<h3>vue jsx写法DOM二</h3>
<renderJsx
refName="JsxUlRef2"
label="2"
/>
</el-card>
</template>
<script setup lang="jsx">
import { ref, onMounted } from 'vue';
import { message } from '@/utils/message';
import styles from './jsx.module.scss';
/**
* 1. jsx中怎么使用element组件
* 2. jsx语法中样式问题全局css modules
* 3. jsx语法中ref的使用
* 4. jsx在template中怎么写才能渲染
*/
defineOptions({
name: 'DemoJsx',
});
const refs = ref({});
const list = ref([
{
name: 'John',
age: 25,
address: 'New York',
},
{
name: 'Jane',
age: 30,
address: 'London',
},
{
name: 'Bob',
age: 28,
address: 'Paris',
},
]);
const renderJsx = (props) => {
console.log(props);
return (
<ul class="demo-jsx-ul">
{list.value.map((item, index) => (
<li key={index}>
<div>姓名: {item.name}</div>
<div>年龄: {item.age}</div>
<div>地址: {item.address}</div>
</li>
))}
<el-button
type="primary"
class="mb-10"
onClick={elementBtnClick}
>
测试添加element组件
</el-button>
<div class={styles.btn}>css modules样式测试</div>
<el-button
ref={(el) => (refs.value[props.refName] = el)}
type="primary"
onClick={() => changeRefText(props.refName, props.label)}
>
测试ref{props.label}
</el-button>
</ul>
);
};
const elementBtnClick = () => {
message('elementBtnClick', { type: 'info' });
};
const changeRefText = (refName, label) => {
refs.value[refName].ref.innerText = `测试ref${label}改名字了`;
};
onMounted(() => {
console.log(refs);
});
</script>
<style lang="scss" scoped>
/** 这里局部样式对jsx组件不会生效 */
// .demo-jsx-ul {
// margin: 0 auto;
// li {
// border: 1px solid #ccc;
// padding-left: 20px;
// margin: 20px 0;
// }
// }
</style>
<style lang="scss">
/** jsxcss module, */
.demo-jsx-ul {
margin: 0 auto;
li {
border: 1px solid #ccc;
padding-left: 20px;
margin: 20px 0;
}
}
</style>