简体   繁体   English

如何在 vue.js 中的组件之间切换?

[英]How to toggle between components in vue.js?

I developed one page called Dashboard.vue and this page contains three child components(Display,sortBooksLowtoHigh,sortBooksHightoLow).我开发了一个名为 Dashboard.vue 的页面,该页面包含三个子组件(Display、sortBooksLowtoHigh、sortBooksHightoLow)。 Dashboard component contains one select options also inside that it have two options "Price:High to Low and Price:Low to High ",仪表板组件还包含一个选择选项,其中有两个选项“价格:从高到低和价格:从低到高”,

if i click on price:LowToHigh option then it hides the Display component and displays the sortBooksLowtoHigh component utpo this it's working fine,如果我点击 price:LowToHigh 选项,那么它会隐藏 Display 组件并显示 sortBooksLowtoHigh 组件,它工作正常,

Now i import one more component called sortBooksHightoLow when i click on "price:High to Low" option it should hides the DisplayComponent and displays the sortBooksHightoLow component.How to acheive this thing please help me现在,当我单击“价格:从高到低”选项时,我又导入了一个名为 sortBooksHightoLow 的组件,它应该隐藏 DisplayComponent 并显示 sortBooksHightoLow 组件。如何实现这件事请帮帮我

Dashboard.vue

<template>
<div class="main">
    <div class="navbar navbar-default navbar-fixed-top">
        <div class="navbar-header">
            <img src="../assets/education.png" alt="notFound" class="education-image" />
        </div>
        <ul class="nav navbar-nav">
            <li>
                <p class="brand">Bookstore</p>
            </li>
        </ul>
        <div class="input-group">
            <i @click="handlesubmit();" class="fas fa-search"></i>
            <div class="form-outline">
                <input type="search"  v-model="name" class="form-control" placeholder='search...' />
            </div>
        </div>

        <ul class="nav navbar-nav navbar-right" id="right-bar">
            <li><a> <i class="far fa-user"></i></a></li>
            <p class="profile-content">profile</p>
            <li><a><i class="fas fa-cart-plus"></i></a></li>
            <p class="cart-content">cart</p>
        </ul>
    </div>
    <div class="mid-body">
        <h6>Books<span class="items">(128items)</span></h6>

     
        <select class="options" @change="applyOption">
            <option disabled value="">Sort by relevance</option>
            <option value="HighToLow">price:High to Low</option>
            <option value="LowToHigh">price:Low to High</option>
        </select>
    </div>



    <DisplayBooks v-show="flag==='noOrder'" />
    <sortBooksLowtoHigh v-show="flag==='lowToHigh'" />
    <sortBooksHightoLow v-show="flag==='highToLow'" />
</div>
</template>

<script>
import service from '../service/User'
import sortBooksLowtoHigh from './sortBooksLowtoHigh.vue'
import sortBooksHightoLow from './sortBooksHightoLow.vue'
import DisplayBooks from './DisplayBooks.vue'
export default {
    components: {
        DisplayBooks,
        sortBooksLowtoHigh,
        sortBooksHightoLow
    },
    data() {
        return {
            flag: 'noOrder',
            brand: 'Bookstore',
            name: '',
           
            visible:true,
            books: [{

            }]
        }
    },
    methods: {
      
        flip() {
            this.flag = !this.flag;
        },
       
        applyOption(evt) {
            if (evt.target.value === "HighToLow") this.flag = 'highToLow';
            else this.flag = 'lowToHigh';
        },
    }

}
</script>

<style lang="scss" scoped>
@import "@/styles/Dashboard.scss";
</style>

sortBooksHightoLow.vue

<template>
<div class="carddisplay-section">
    <div v-for="book in books" :key="book.id" class="card book">
        <div class="image-section">
            <div class="image-container">
                <img v-bind:src="book.file" />
            </div>
        </div>
        <div class="title-section">
            {{book.name}}
        </div>
        <div class="author-section">
            by {{book.author}}
        </div>
        <div class="price-section">
            Rs. {{book.price}}<label class="default">(2000)</label>
            <button v-if="flag" class="btn-grp" type="submit" @click="handlesubmit();Togglebtn();">close</button>
        </div>
        <div class="buttons">
            <div class="button-groups">
                <button type="button" @click="toggle(book.id);flip(book.id);" v-if="state==true" class="AddBag">Add to Bag</button>
                <button v-if="state==true" class="wishlist">wishlist</button>
            </div>
            <div v-if="state==false" class="AddedBag">
                <button class="big-btn">Added to Bag</button>
            </div>
        </div>
    </div>

</div>
</template>

<script>
import service from '../service/User'

export default {

    data() {
        return {
            result: 0,
            authorPrefix: 'by',
            pricePrefix: 'Rs.',
            defaultStrikePrice: '(2000)',
            buttonValue: 'close',
            flag: true,
            state: true,
            clickedCard: '',
            books: [{
                id: 0,
                file: 'https://images-na.ssl-images-amazon.com/images/I/41MdP5Tn0wL._SX258_BO1,204,203,200_.jpg',
                name: 'High to Low',
                author: 'Saioii',
                price: '1500'
            }, ]
        }
    },
    methods: {
        toggle(id) {
            this.clickedCard = id;
            // this.card.content = this.notes.filter((note) => note.id === id);
            console.log(this.clickedCard);
        },

        flip() {
            this.state = !this.state;
        },
        Togglebtn() {
            this.flag = !this.flag;
        },
        handlesubmit() {
            service.userDisplayBooksHightoLow().then(response => {
                this.books.push(...response.data);
            })
        },
    }
}
</script>

<style lang="scss" scoped>
@import "@/styles/DisplayBooks.scss";
</style>

sortBooksLowtoHigh.vue

<template>
<div class="carddisplay-section">
    <div v-for="book in books" :key="book.id" class="card book">
        <div class="image-section">
            <div class="image-container">
                <img  v-bind:src="book.file" />
            </div>
        </div>
        <div class="title-section">
            {{book.name}}
        </div>
        <div class="author-section">
            by {{book.author}}
        </div>
        <div class="price-section">
            Rs. {{book.price}}<label class="default">(2000)</label>
            <button v-if="flag" class="btn-grp" type="submit" @click="handlesubmit();Togglebtn();">close</button>
        </div>
        <div class="buttons">
            <div class="button-groups">
                <button type="button"  @click="toggle(book.id);flip(book.id);"  v-if="state==true" class="AddBag">Add to Bag</button>
                <button v-if="state==true" class="wishlist">wishlist</button>
            </div>
            <div v-if="state==false" class="AddedBag">
                <button class="big-btn">Added to Bag</button>
            </div>
        </div>
    </div>

</div>
</template>

<script>
import service from '../service/User'

export default {

    data() {
        return {
            result: 0,
            authorPrefix: 'by',
            pricePrefix: 'Rs.',
            defaultStrikePrice: '(2000)',
            buttonValue: 'close',
            flag: true,
            state: true,
            clickedCard: '',
            books: [{
                id: 0,
                file: 'https://images-na.ssl-images-amazon.com/images/I/41MdP5Tn0wL._SX258_BO1,204,203,200_.jpg',
                name: 'Default Card',
                author: 'Sai',
                price: '..'
            }, ]
        }
    },
    methods: {
         toggle(id) {
            
            this.clickedCard = id;
            // this.card.content = this.notes.filter((note) => note.id === id);
          console.log(this.clickedCard);
        },
    
        flip() {
            this.state = !this.state;
        },
        Togglebtn() {
            this.flag = !this.flag;
        },
        handlesubmit() {
            service.userDisplayBooksLowtoHigh().then(response => {
                this.books.push(...response.data);  
                console.log(this.response);   
            })
        },
    }
}
</script>

<style lang="scss" scoped>
    @import "@/styles/DisplayBooks.scss";
</style>

emmmm... HightoLow => HighToLow。

There can be several methods, in my opinion, to achieve conditional rendering of components which I think your question asks for.在我看来,可以有几种方法来实现我认为您的问题所要求的组件的条件渲染。 Two of them which are highly useful are:其中两个非常有用的是:

  1. Using v-if and v-else where you must define a flag that handles the logic for component rendering.使用v-ifv-else必须定义一个标志来处理组件渲染的逻辑。 Also, wrapping them in a transition tag would a good idea to make the switch with a transition.此外,将它们包装在过渡标签中是一个好主意,可以通过过渡进行切换。
<transition>
  <component1 v-if="flag" />
  <component2 v-else />
</transition>
  1. Dynamic Components, we use the component tag and is attribute.动态组件,我们使用component标签和is The component can then be switched using the name of the component.然后可以使用组件的名称切换组件。
<component is="nameofComponent" />

You can read more about dynamic components in vuejs docs .您可以在 vuejs 文档中阅读更多关于动态组件的信息。

While the dynamic component looks neat, a switch with transition can be a nice addition.虽然动态组件看起来很整洁,但带有过渡的开关可能是一个不错的补充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM