代码之家  ›  专栏  ›  技术社区  ›  Thibault Dumas

虚拟用户.js页脚组件作用于其他组件?

  •  0
  • Thibault Dumas  · 技术社区  · 7 年前

    基本上,在应用程序的某些页面上,我可以将项目添加到购物车中,通过页脚中的图标,我可以访问项目列表,如果我愿意,可以删除它们。

    我的购物车显示为对话框全屏,上面是我的应用程序。

    这是我的页脚部分(与购物车一起):

    var footer = Vue.component("iteck-footer", {
        template: `<div>
        <v-footer fixed height="auto" color="teal" dark>
        <v-layout class="pl-2 pr-2" justify-center row wrap>
    
                <v-dialog fullscreen v-model="dialog" transition="dialog-bottom-transition">
                    <v-toolbar fixed dense dark color="teal">
                      <v-spacer></v-spacer>
                      <v-toolbar-title>Panier</v-toolbar-title>
                      <v-spacer></v-spacer>
                      <v-toolbar-items>
                        <v-btn icon dark @click="dialog = false">
                            <v-icon>close</v-icon>
                          </v-btn>
                      </v-toolbar-items>
                    </v-toolbar>
                    <div class="content-with-header-footer">
                        <div v-if="articles.length > 0">
                            <div v-for="article in articles" :key="article.LIG">
                                <v-checkbox class="inputs-panier pa-0 ma-0 " v-model="article.selected">
                                    <template slot="label">
                                    <div>{{article.DESIG}} | <b>Qté.</b> : {{quantite(article.QTESAI)}}</div>
                                    <div>{{article.CLE}} {{article.CODBAR}}</div>
                                    </template>
                                </v-checkbox>
                            </div>
                        </div>
                        <div class="text-xs-center mt-4" v-else>
                            <h2 class="red--text">Panier vide</h2>
                        </div>
                    </div>
                    <v-footer fixed height="auto" color="teal" dark>
                        <v-btn @click="deleteLignesPanier()" class="mx-0" icon>
                            <v-icon>delete</v-icon>
                        </v-btn>
    
                        <v-spacer></v-spacer>
    
                        <v-btn @click="validerPanier()" class="mx-0" icon>
                            <i class="material-icons">check</i>
                        </v-btn>
    
                    </v-footer>
                </v-dialog>
    
            <v-btn style="position:relative;" @click="openMenu()" class="mx-0" icon>
                <i class="material-icons">shopping_basket</i>
                <span id="nb_articles">
                    {{articles.length}}
                </span>
            </v-btn>
    
            <v-spacer></v-spacer>
    
            <v-btn @click="validerPanier()" class="mx-0" icon>
                <i class="material-icons">check</i>
            </v-btn>
    
          <v-dialog v-model="modal" max-width="290">
          <v-card>
            <v-card-text>
              {{modalMessage}}
            </v-card-text>
    
            <v-card-actions>
              <v-btn color="green darken-1" flat="flat" @click="modal = false; modalMessage = ''">
                OK
              </v-btn>
            </v-card-actions>
          </v-card>
        </v-dialog>
    
        </v-layout>
      </v-footer>
    </div>`,
        props: ["menu"],
        data() {
            return {
                modal : false,
                modalMessage : "",
                entete: null,
                articles: [],
                dialog: false
            }
        },
        mounted() {
            this.getPanier()
        },
        watch: {
            // whenever question changes, this function will run
            dialog: function (newValue, oldValue) {
                if(newValue === false && this.articles.length === 0)
                    location.reload();
            }
        },
        methods: {
            async openMenu() {
                this.articles = [];
                this.entete = null;
                if(this.dialog === false) {
                    //Récuperation du panier en cours.
                    this.getPanier();
                }
                this.dialog = true;
            },
            async deleteLignesPanier() {
                let lignes = "";
    
                for (let i=0; i < this.articles.length; i++ ) {
                    if(this.articles[i].selected === true) {
                        if(lignes === "")
                            lignes = lignes + this.articles[i].LIGNE;
                        else
                            lignes = lignes + "," + this.articles[i].LIGNE;
                    }
                }
    
                if(lignes !== "") {
    
                    //On envoie la requête au serveur.
                    let data = await wspda.deleteLignesPanier(this.menu,lignes);
    
                    if(data == 'true')
                    {
                        console.log('OUI');
                        console.log('length',this.articles.length);
                        console.log('articles',this.articles);
                        for (let i = this.articles.length -1; i >= 0; i-- ) {
                            if(this.articles[i].selected === true) {
                                this.articles.splice(i,1);
                            }
                        }
                    }
                    else
                    {
                        console.log(data);
                    }
                }
    
            },
            async validerPanier() {
                if(this.articles.length > 0) {
                    let data  = await wspda.validerPanier(this.menu,"","False");
                    this.modal = true;
                    this.modalMessage = data;
                    this.articles = [];
                }
            },
            async getPanier() {
                let data = await wspda.getPanier(this.menu);
                if(data.ProDataSet && data.ProDataSet.PANIERENT && data.ProDataSet.PANIERLIG) {
    
                    if(Array.isArray(data.ProDataSet.PANIERLIG)) {
                        this.articles = data.ProDataSet.PANIERLIG;
                    } else {
                        this.articles.push(data.ProDataSet.PANIERLIG);
                    }
    
                    this.entete = data.ProDataSet.PANIERENT;
    
                    for (let i=0; i<this.articles; i++ ) {
                        this.articles[i].selected = false;
                    }
                }
            },
            quantite(qtesai) {
                return (qtesai % 1) === 0 ? Math.round(qtesai) : qtesai;
            }
        }
    });
    

    正如你所见,在我的观察者,在关闭购物车,我可以重新加载页面。。。但是有一种方法,当购物车是空的(删除项目),重新加载我的对话框后面的当前组件??找不到合适的方法。

    以及我当前运行的组件(仅举一个例子):

    var spaHome = Vue.component("Home", {
        template: `<div>
            <v-container fill-height>
                <v-layout>
                    <v-flex>
                    <v-list>
                        <v-list-tile class="menu-home"
                           v-for="(item,index) in menus" :key="index" @click="$router.push(item.path)" v-if="item.value">
                            <!--<v-list-tile-action>
                                <v-icon>{{ item.icon }}</v-icon>
                            </v-list-tile-action>-->
                            <v-list-tile-content class="teal darken-3 white--text">
                                <v-list-tile-title class="text-xs-center">{{ item.label }}</v-list-tile-title>
                            </v-list-tile-content>
                        </v-list-tile>
                    </v-list>
                    </v-flex>
                </v-layout>
            </v-container>
    </div>`,
        props: ["title"],
        $_veeValidate: {
            validator: "new"
        },
        data() {
            return {
                menus: conf.Menus,
            }
        },
        methods: {
        }
    }); 
    

    0 回复  |  直到 7 年前