<template> <div id="app"> <!-- 3.向子组件传输数据 --> <Time :times="ptimes" /> </div></template><script>// 1.引入组件import Time from "./components/time.vue";export default { name: "App", components: { // 注册组件 Time, }, data() { return { ptimes: ["一天", "一周", "一月", "一年"], }; },};</script><style>#app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px;}</style>
<template> <div class="chooser-components"> <ul class="chooser-list"> <!-- :class 是动态class 满足nowIndex===index 则动态的添加上active样式 --> <li @click="clickHandle(index)" :class="{ active: nowIndex === index }" v-for="(item, index) in times" :key="index" > {{ item }} </li> </ul> </div></template><script>export default { // 4.接收父组件传输过来的数据 props: ["times"], data() { return { nowIndex: 0, }; }, methods: { clickHandle(index) { this.nowIndex = index; }, },};</script><style scoped>li { list-style-type: none;}.chooser-component { position: relative; display: inline-block;}.chooser-list li { display: inline-block; padding: 0 8px; border: 1px solid #e3e3e3; height: 25px; line-height: 25px; margin: 0 5px; border-radius: 3px; /* 设置圆角弧度 */ align-items: center; width: 60px; cursor: pointer; /* 小手样式 */}.active { background-color: #4fc08d; border-color: #4fc08d; color: #fff;}</style>