簡體   English   中英

如何在 vue.js 中的組件之間切換?

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

我開發了一個名為 Dashboard.vue 的頁面,該頁面包含三個子組件(Display、sortBooksLowtoHigh、sortBooksHightoLow)。 儀表板組件還包含一個選擇選項,其中有兩個選項“價格:從高到低和價格:從低到高”,

如果我點擊 price:LowToHigh 選項,那么它會隱藏 Display 組件並顯示 sortBooksLowtoHigh 組件,它工作正常,

現在,當我單擊“價格:從高到低”選項時,我又導入了一個名為 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。

在我看來,可以有幾種方法來實現我認為您的問題所要求的組件的條件渲染。 其中兩個非常有用的是:

  1. 使用v-ifv-else必須定義一個標志來處理組件渲染的邏輯。 此外,將它們包裝在過渡標簽中是一個好主意,可以通過過渡進行切換。
<transition>
  <component1 v-if="flag" />
  <component2 v-else />
</transition>
  1. 動態組件,我們使用component標簽和is 然后可以使用組件的名稱切換組件。
<component is="nameofComponent" />

您可以在 vuejs 文檔中閱讀更多關於動態組件的信息。

雖然動態組件看起來很整潔,但帶有過渡的開關可能是一個不錯的補充。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM