一、三目运算

  1. <template>
  2. <div>
  3. <button @click="handleToggle">toggle</button>
  4. <component :is="isToggle?one:two"></component>
  5. </div>
  6. </template>
  7. <script>
  8. import One from "../components/One.vue";
  9. import Two from '../components/Two.vue'
  10. export default {
  11. data() {
  12. return {
  13. isToggle:false,
  14. one: "One",
  15. two:"Two"
  16. };
  17. },
  18. components: {
  19. One,
  20. Two
  21. },
  22. methods:{
  23. handleToggle(){
  24. this.isToggle = !this.isToggle
  25. }
  26. }
  27. };
  28. </script>

二、ref

$refs:一个对象,持有已注册过ref的所有子组件
ref:被用来给元素或者子组件注册引用信息。引用信息将会注册在父组件的$refs对象上。如果在普通的DOM元素上使用,引用指向的就是DOM元素;如果用在子组件上,引用就指向组件实例;
第一步:给子组件添加ref属性
第二步:添加点击事件,触发动态组件显示组件

  1. <view class="content-main perCenter">
  2. <view class="tab-list df-c">
  3. <view class="tab-item" @click="handleShowWel">
  4. <img src="~@/static/img/icon-welfare.png" alt="" class="icon" />
  5. 福利记录
  6. </view>
  7. <view class="tab-item" @click="handleShowPri">
  8. <img src="~@/static/img/icon-prize.png" alt="" class="icon" />
  9. 奖品记录
  10. </view>
  11. </view>
  12. <prizeRecords ref="priRecords"></prizeRecords>
  13. <welfareList ref="welRecords"></welfareList>
  14. </view>
  1. export default class extends Vue {
  2. handleShowPri() {
  3. const priRecords: any = this.$refs.priRecords;
  4. priRecords.show();
  5. }
  6. handleShowWel() {
  7. const welRecords: any = this.$refs.welRecords;
  8. welRecords.show();
  9. }
  10. }