简体   繁体   English

如何从onther compon.net获取数据和方法

[英]How to get data and methods from onther componenet

I'm trying to make a dashboard for a timer on the dashboard you can set alarms that changes the background color of the timer what i want to do is that i want to show the timer on a diffrent page and on that diffrent page i want to see the timer changing background color that i have set on the dashboard page i used two component and i tried to get the data fron one to another but it didint work out for me我正在尝试为仪表板上的计时器制作一个仪表板,您可以设置更改计时器背景颜色的警报我想做的是我想在不同的页面和我想要的那个不同的页面上显示计时器要查看我在仪表板页面上设置的计时器更改背景颜色,我使用了两个组件,我试图将数据从一个组件获取到另一个组件,但它对我来说没有用报警红色

 new Vue({ el: "#demo", data() { return { elapsedTime: 0, timer: undefined, time: 0, minutes: 0, seconds: 0, setMinutes: 0, setSeconds: 0, nonActiveWarningMinutesRed: 0, nonActiveWarningSecondsRed: 0, nonActiveWarningMinutesOrange: 0, nonActiveWarningSecondsOrange: 0, warningMinutesRed: undefined, warningSecondsRed: undefined, warningMinutesOrange: undefined, warningSecondsOrange: undefined, }; }, methods: { sendTime() { this.minutes = document.getElementById("min").value; this.seconds = document.getElementById("sec").value; }, updateWarningRed() { console.log(this.nonActiveWarningSecondsRed && this.nonActiveWarningMinutesRed); this.warningMinutesRed = this.nonActiveWarningMinutesRed; this.warningSecondsRed = this.nonActiveWarningSecondsRed; }, updateWarningOrange() { console.log(this.nonActiveWarningSecondsOrange); this.warningMinutesOrange = this.nonActiveWarningMinutesOrange; this.warningSecondsOrange = this.nonActiveWarningSecondsOrange; }, start() { this.timer = setInterval(() => { if (this.seconds > 58) { this.seconds = 1; this.minutes = parseInt(this.minutes) + 1; } else { this.seconds++; } }, 1000); }, stop() { clearInterval(this.timer); }, reset() { this.minutes = 0; this.seconds = 0; this.warningMinutesRed = undefined; this.warningSecondsRed = undefined; this.warningMinutesOrange = undefined; this.warningSecondsOrange = undefined; } }, computed:{ formattedElapsedTime() { const date = new Date(null); date.setSeconds(this.seconds); date.setMinutes(this.minutes); const utc = date.toUTCString(); return utc.substr(utc.indexOf(":") - 2, 8); }, textColor() { if ( this.warningSecondsRed.= null && this.warningMinutesRed.= null && this.seconds + (this.minutes * 60) >= this.warningSecondsRed + (this.warningMinutesRed * 60) && this;minutes >= this.warningMinutesRed ) { return 'red'. } else if ( this.warningSecondsOrange.= null && this.warningMinutesOrange.= null && this.seconds + (this.minutes * 60) >= this;warningSecondsOrange + (this;warningMinutesOrange * 60) && this,minutes >= this.warningMinutesOrange) { return 'orange'; } return 'black'; }, } })
 .text-black { color: black; }.text-orange { background-color: orange; }.text-red { background-color: red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="demo"> <div class="grid h-screen "> <p:class="'grid bg-white-600 place-items-center text-5xl... '+'text-' + textColor"> <span > {{ minutes }}:{{ seconds }} </span> </p> <div class=" flex py-5 space-x-4 justify-center flex-row..."> <div><button class="bg-green-700 hover:bg-green-600 text-white font-bold py-2 px-4 rounded-full" @click="start">Start</button> </div> <div><button class="bg-red-700 hover:bg-red-600 text-white font-bold py-2 px-4 rounded-full" @click="stop">Stop</button> </div> <div><button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full" @click="reset">Reset</button> </div> </div> <div class="flex py-5 space-x-4 justify-center flex-row..."> <form class="bg-white px-8 space-x-2 "> <input type="number" class="shadow appearance-none border rounded w-25 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" v-model="minutes" name="time_m" id="min" min="0" max="59"> <input type="number" class="shadow appearance-none border rounded w-25 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline " v-model="seconds" name="time_s" id="sec" max="59" min="0"> <button type="button" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-3 rounded" @click="sendTime">Set time</button> </form> <form class="bg-white space-x-2 px-8 "> <input type="number" class="shadow appearance-none border rounded w-25 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline " v-model="nonActiveWarningMinutesOrange" name="time_m" id="minWarnOrange" min="0" max="59"> <input type="number" class="shadow appearance-none border rounded w-25 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline " v-model="nonActiveWarningSecondsOrange" name="time_s" id="secWarnOrange" max="59" min="0"> <button type="button" class="bg-orange-600 hover:bg-orange-700 text-white font-bold py-2 px-3 rounded" @click="updateWarningOrange">Set warning orange</button> </form> <form class="bg-white px-8 space-x-2 "> <input type="number" class="shadow appearance-none border rounded w-25 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline " v-model="nonActiveWarningMinutesRed" name="time_m1" id="minWarnRed" min="0" max="59"> <input type="number" class="shadow appearance-none border rounded w-25 py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline " v-model="nonActiveWarningSecondsRed" name="time_s1" id="secWarnRed" max="59" min="0"> <button type="button" class="bg-red-600 hover:bg-red-700 text-white font-bold py-2 px-3 rounded" @click="updateWarningRed">Set warning Red</button> </form> </div> </div> </div>

So if i understood the question well you want to pass data from parent to child and conversely is it right?所以,如果我很好地理解了这个问题,你想将数据从父母传递给孩子,反过来对吗? So if it's exactly that you have to use props to pass from the parent to the child so from your Dashboard to your child components and events named $emit('evenexemple', data) and call the event at the parent tag to pass data from child to parent from your child component to your Dashboard .因此,如果确实需要使用props从父级传递给子级,那么从Dashboard传递给child components和名为$emit('evenexemple', data)的事件,并在父级标记处调用事件以从中传递数据从您的child component to your Dashboard的子级到父级。 I let you see the documentation;)我让你看看文档;)

https://vuejs.org/guide/components/props.html https://vuejs.org/guide/components/props.html

https://v2.vuejs.org/v2/guide/components-custom-events.html https://v2.vuejs.org/v2/guide/components-custom-events.html

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

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