Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue if...else if 简便写法 #19

Closed
xiaweiss opened this issue Jan 2, 2020 · 1 comment
Closed

Vue if...else if 简便写法 #19

xiaweiss opened this issue Jan 2, 2020 · 1 comment
Labels

Comments

@xiaweiss
Copy link
Owner

xiaweiss commented Jan 2, 2020

No description provided.

@xiaweiss
Copy link
Owner Author

xiaweiss commented Jan 2, 2020

<template>
  <SubjectMobile v-if="isMobile" :otherProps="otherProps" />
  <SubjectPC v-else :otherProps="otherProps" />
</template>

<script>
import SubjectMobile from './mobile/Index.vue'
import SubjectPC from './pc/Index.vue'
import {mapState} from 'vuex'

export default {
  name: 'subject-page-index',
  components: {
    SubjectMobile,
    SubjectPC
  },
  computed: {
    ...mapState(['isMobile']),
    com () {
      return this.isMobile ? SubjectMobile : SubjectPC
    },
    otherProps () {
      return 'otherProps'
    }
  }
}
</script>

等价写法

<template>
  <component :is="com" :otherProps="otherProps"/>
</template>

<script>
import SubjectMobile from './mobile/Index.vue'
import SubjectPC from './pc/Index.vue'
import {mapState} from 'vuex'

export default {
  name: 'subject-page-index',
  components: {
    SubjectMobile,
    SubjectPC
  },
  computed: {
    ...mapState(['isMobile']),
    com () {
      return this.isMobile ? SubjectMobile : SubjectPC
    },
    otherProps () {
      return 'otherProps'
    }
  }
}
</script>

@xiaweiss xiaweiss changed the title Vue Vue if...else if 简便写法 Jan 2, 2020
@xiaweiss xiaweiss added the Web label Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant