首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将所选项目置为当前

将所选项目置为当前
EN

Stack Overflow用户
提问于 2021-09-29 22:02:35
回答 1查看 47关注 0票数 0

编辑:3个最可能的文件的完整代码可以在这里找到。https://codepen.io/arcticmedia-ryan/project/editor/ZEQwmN

我不知道如何最好地解释这一点,所以如果你想要更多,请告诉我。我正在构建一个聊天室应用程序。当一个人打开主vue时,他们会看到一个“房间列表”-从那里他们可以点击进入并加入房间。问题是,当他们单击加入时,它会打开聊天室的选项卡,但不会将其激活。我觉得我只是在为一个简单的问题而苦思冥想

有一个名为"SelectedRoomID“的道具需要从”未定义“更新到从列表中选择的房间。

RoomList.vue

代码语言:javascript
运行
复制
<!-- This example requires Tailwind CSS v2.0+ -->
<template>
    <TransitionRoot as="template" :show="isOpen">
        <Dialog as="div" class="fixed z-10 inset-0 overflow-y-auto" @close="$emit('onClose')">
            <div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
                <TransitionChild as="template" enter="ease-out duration-300" enter-from="opacity-0"
                                 enter-to="opacity-100" leave="ease-in duration-300" leave-from="opacity-100"
                                 leave-to="opacity-0">
                    <DialogOverlay class="fixed inset-0 bg-gray-700 bg-opacity-75 transition-opacity"/>
                </TransitionChild>

                <!-- This element is to trick the browser into centering the modal contents. -->
                <span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">&#8203;</span>
                <TransitionChild as="template" enter="ease-out duration-300"
                                 enter-from="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
                                 enter-to="opacity-100 translate-y-0 sm:scale-100" leave="ease-in duration-300"
                                 leave-from="opacity-100 translate-y-0 sm:scale-100"
                                 leave-to="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95">
                    <div class="inline-block align-bottom bg-black bg-opacity-20 rounded-lg px-6 pt-5 pb-4 text-left overflow-hidden shadow-2xl transform transition-all sm:my-8 sm:align-middle sm:p-6">

                        <div class="sm:flex sm:items-start">
                            <div class="flex flex-col">
                                <div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
                                    <div class="align-middle inline-block min-w-full sm:px-4 lg:px-5">
                                        <div class="shadow overflow-hidden border-8 border-gray-800 dark:border-gray-900 sm:rounded-lg">
                                            <table class="min-w-full divide-y divide-gray-800">
                                                <thead class="bg-gray-800 dark:bg-gray-900">
                                                <tr>
                                                    <th scope="col"
                                                        class="px-6 py-3 text-left text-s font-medium text-gray-50 uppercase tracking-wider">
                                                        Room
                                                    </th>
                                                    <th scope="col"
                                                        class="px-6 py-3 text-left text-s font-medium text-gray-50 uppercase tracking-wider">
                                                        Description
                                                    </th>
                                                    <th scope="col"
                                                        class="px-6 py-3 text-left text-s font-medium text-gray-50 uppercase tracking-wider">
                                                        Type
                                                    </th>
                                                    <th scope="col"
                                                        class="px-6 py-3 text-left text-s font-medium text-gray-50 uppercase tracking-wider">
                                                        Users
                                                    </th>
                                                    <th scope="col" class="relative px-6 py-3">
                                                        <button type="button"
                                                                class="bg-white rounded-md text-gray-400 hover:text-gray-500 "
                                                                @click="$emit('onClose')">
                                                            <div class="hidden sm:block absolute top-0 right-0 pt-4 pr-4">
                                                                <span class="sr-only">Close</span>
                                                                <XIcon class="top-1 right-1 absolute h-6 w-6 text-gray-50 " aria-hidden="true"/>
                                                            </div>
                                                        </button>
                                                        
                                                        <span class="sr-only">Enter</span>
                                                        
                                                    </th>
                                                </tr>
                                                </thead>
                                                <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-300 dark:divide-gray-600">
                                                <tr v-for="room in $store.state.rooms.rooms" :key="room.id">
                                                    <td class="px-6 py-4 whitespace-nowrap">
                                                        <div class="flex items-center">
                                                            <div class="flex-shrink-0 h-10 w-10">
                                                                <img class="h-10 w-10 rounded-full" :src="room.image"
                                                                     alt=""/>
                                                            </div>
                                                            <div class="ml-4">
                                                                <div class="text-md font-medium text-gray-900 dark:text-gray-50">
                                                                    {{ room.name }}
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </td>
                                                    <td class="px-6 py-4 whitespace-nowrap">
                                                        <div class="text-md text-gray-900 dark:text-gray-50">{{ room.description }}</div>
                                                    </td>
                                                    <td class="px-2 py-4 whitespace-nowrap">
                                                        <span class="px-2 inline-flex text-s leading-5 font-medium rounded-full">
                                                            <div v-if="room.is_private === true" class="rounded-full py-1 px-2 bg-red-100 text-red-800 dark:bg-red-800 dark:text-red-100">
                                                                        <span>Private</span>
                                                                    </div>
                                                                    <div v-else-if="room.is_private === false" class="rounded-full py-1 px-2 bg-green-100 text-green-800 dark:bg-green-800 dark:text-green-100">
                                                                        <span>Public</span>
                                                                    </div>
                                                                    <div v-else-if="room.is_private === 'true'" class="rounded-full py-1 px-2 bg-red-100 text-red-800 dark:bg-red-800 dark:text-red-100">
                                                                        <span>Private</span>
                                                                    </div>
                                                                    <div v-else-if="room.is_private === 'false'" class="rounded-full py-1 px-2 bg-green-100 text-green-800 dark:bg-green-800 dark:text-green-100">
                                                                        <span>Public</span>
                                                                </div>
                                                        </span>
                                                    </td>
                                                    <td class="px-6 py-4 whitespace-nowrap text-center">
                                                        <a class="text-md font-medium text-gray-900 dark:text-gray-50">99</a>
                                                      </td>
                                                    <td class="px-6 py-4 whitespace-nowrap text-right text-md font-medium">
                                                        <div class="flex rounded-full py-2 px-4 bg-indigo-700 hover:bg-indigo-900">
                                                        <a href="#" @click="joinRoom(room)"
                                                           class="text-white">Enter</a>
                                                        </div>
                                                    </td>
                                                </tr>
                                                </tbody>
                                            </table>
                                            <div class="bg-gray-800 dark:bg-gray-900">
                                                <div class="flex justify-end px-4 py-3 text-left text-s font-medium text-gray-50 uppercase tracking-wider">
                                                    <span class=" px-2 pt-2 text-xs leading-5 font-semibold rounded-full">
                                                    <a href="#">
                                                    <div class="flex rounded-full py-1 px-2 bg-green-700 text-green-100 hover:bg-green-800">
                                                        <PlusIcon class="h-5 w-5"/><span class="text-sm font-bold pl-0.5">Create Room</span>
                                                    </div>
                                                    </a>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </TransitionChild>
            </div>
        </Dialog>
    </TransitionRoot>
</template>

<script>
import {Dialog, DialogOverlay, DialogTitle, TransitionChild, TransitionRoot} from '@headlessui/vue'
import {ExclamationIcon, XIcon, PlusIcon} from '@heroicons/vue/outline'
import {types as RoomTypes} from "../../../Store/modules/rooms/rooms.types";

export default {
    components: {
        Dialog,
        DialogOverlay,
        DialogTitle,
        TransitionChild,
        TransitionRoot,
        ExclamationIcon,
        XIcon,
        PlusIcon,
    },
    props: ['rooms', 'selectedRoomId'],
    emits: ['onSelectRoom', 'onLeaveRoom', 'onClose'],
    methods: {
        joinRoom(room) {
            this.$store.dispatch(RoomTypes.JOIN_ROOM, room);
            this.selectedRoomId = this.room;
            this.$emit('onClose');
        }
    },
    props: {
        isOpen: Boolean,
        usersCount: Boolean
    },
}
</script>

Main.vue代码片段

代码语言:javascript
运行
复制
<section aria-labelledby="primary-heading"
                             class="dark:bg-gray-900 min-w-0 flex-1 h-full flex flex-col overflow-hidden lg:order-last">
                        <room-tabs :selectedRoomId="currentRoom.id" :rooms="joinedRooms"
                                   @onSelectRoom="currentRoom = $event" @onLeaveRoom="leaveRoom($event)"/>
                        <message-container :messages="messages"
                                           class="h-full pb-2 mb-2 pt-0 mt-0 shadow-md overflow-y-auto"/>
                        <input-box
                            :room="currentRoom"
                            v-on:messagesent="getMessages"
                            class="dark:bg-gray-800 px-6 pb-6 pt-4 dark:border-t-2 dark:border-gray-600 bottom-0 sticky"
                        />
                    </section>

我知道它的内部方法:需要更改的joinRoom,我知道this.selectedRoomId在哪里是错的-我只是不知道我需要更改/修复什么才能使它在您单击join时将选定的房间变为活动房间。

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2021-09-30 02:40:31

我将使用mapState有效地监视由this.$store.dispatch(RoomTypes.JOIN_ROOM, room);更新的存储中的值

因此,在需要知道此值的组件中:

代码语言:javascript
运行
复制
 computed: {
    ...mapState(['STORE_NAME', 'STORE_VALUE']),
  },

然后,您应该能够使用this.STORE_VALUE作为动态更新值

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69383803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档