- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我创建了一个基本的 CLI 结构环境。我有一个显示消息/警报的组件,即:登录失败等……
由于这个组件将在整个应用程序中重复使用,我想将它导入根 App.vue 文件并在那里进行处理。它正在工作......有点。
它可以很好地显示消息/警报,但我希望它在设定的秒数后隐藏/消失/消失。或者单击一个按钮,它会隐藏/消失/消失 - 我在下面的警报组件示例中显示了这一点。在一定的预定义时间后我根本无法隐藏,点击和隐藏有效,但取消隐藏也会产生问题。我在 App.vue 文件中使用 setTimeout 方法在 5 秒后自动隐藏,但什么也没有发生我认为该方法在导入 Alert 模块之前触发……我想。
到目前为止,这是我的代码......看起来很简单,但在过去的几个小时里它一直在破坏我的大脑:
应用组件:
<template>
<div id="app">
<alert v-if="alert" v-bind:message="alert"></alert>
<router-view></router-view>
</div>
</template>
<script>
import Alert from './components/frontend/alert'
export default {
name: 'App',
data() {
return {
alert: ''
}
},
updated: function() {
if(this.$route.query.alert){
this.alert = this.$route.query.alert;
// This was for me to test the click even - PREFER AUTO HIDE AFTER A FEW SECONDS
// This returns an error on form submit - see error below
document.querySelector('.alert').style.display = 'block';
}
},
components: {
'alert': Alert
}
}
</script>
这是警报组件:
<template>
<div class="alert">
<p class="text-brand m-20">
<button class="btn btn-small btn-brand" v-on:click="hideAlert()">Close</button>
{{message}}
</p>
</div>
</template>
<script>
export default {
name: 'alert',
props: ['message'],
data () {
return {
}
},
methods: {
hideAlert() {
// This was for me to test the click even - PREFER AUTO HIDE AFTER A FEW SECONDS
document.querySelector('.alert').style.display = 'none';
}
}
}
</script>
使用点击隐藏时出错 - 来自 App.vue 文件:
[Vue warn]: Error in updated hook: "TypeError: Cannot read property 'style' of null"
found in
---> <App> at src/App.vue
<Root>
如何让 Alert 组件在 App 根组件中隐藏起来,比方说 5 秒?这是我的首选方法,否则我该怎么做才能让点击和隐藏正常工作?
非常感谢!
最佳答案
document.querySelector('.alert').style.display = 'none';
不要这样做。你不应该在方法中操作 DOM,只能在指令和生命周期 Hook 等规定的地方操作。在它们之外,Vue 期望控制 DOM。
你可以控制inline styles使用你的 View 模型。你也可以做 conditional rendering with v-if
. Vue 方法是让您操纵模型并让 Vue 使 DOM 反射(reflect)它。
我已将您的代码改编为下面的可运行代码段。由于您将 hideAlert 方法放在组件中,因此我将关联的 v-if
放在那里。测试是 message
(prop)是否有值,所以关闭就是让父级清除消息。这是使用 the .sync
modifier 处理的标准通信功能.
关闭按钮调用 hideAlert
方法,我还放置了一个观察器,以便在设置新消息时等待 5 秒并调用 hideAlert
。
Alert 组件是独立的;例如,它的 prop 如何获取它的值,父级是否从路由器组件获取它并不重要,重要的只是它是否有值。
const Alert = {
template: '#alert-template',
props: ['message'],
methods: {
hideAlert() {
// Tell the parent to clear the message
this.$emit('update:message', '');
}
},
watch: {
message(newValue) {
// Close after 5 seconds
if (newValue) {
setTimeout(this.hideAlert, 5000);
}
}
}
};
new Vue({
el: '#app',
data() {
return {
alert: ''
}
},
components: {
'alert': Alert
},
mounted() {
// If alert has a value, it will display. If not, not.
setTimeout(() => {
this.alert = 'Now you have a message';
}, 500);
}
});
<script src="//unpkg.com/vue@latest/dist/vue.js"></script>
<div id="app">
<alert v-bind:message.sync="alert"></alert>
</div>
<template id="alert-template">
<div v-if="message" class="alert">
<p class="text-brand m-20">
<button class="btn btn-small btn-brand" v-on:click="hideAlert()">Close</button>
{{message}}
</p>
</div>
</template>
关于javascript - VueJS 显示和隐藏消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48481357/
代码如下: http://jsfiddle.net/t2nite/KCY8g/ 我正在使用 jquery 创建这些隐藏框。 每个框都有一些文本和一个“显示”和“隐藏”按钮。我正在尝试创建一个“显示/隐
我正在尝试做某事。如果单击一个添加 #add-conferance 然后菜单将显示.add-contact。当点击隐藏然后它显示隐藏。我也将 setTimeout 设置为 7sec,但我希望当我的鼠标
我有一个多步骤(多页?)表单,只要用户按下“下一步”或“上一步”按钮,表单字段就会通过 div 显示和隐藏。 我只想禁用第一个 div (div id="page1"class="pageform")
我有一个使用 IIS 6 和 7 的当前系统,用 ASP.NET 和 .NET 4 中的 C# 编写。 My purpose is to hide the url completely (as per
我正在建立一个网站,并有一个幻灯片。幻灯片有标题和索引,覆盖整个页面。当覆盖被激活时,标题需要消失。当覆盖层被停用时,通过单击退出按钮、缩略图链接或菜单链接,字幕必须返回。 这就是我目前所拥有的
我正在尝试为显示/隐藏功能制作简单的 jquery 代码。但我仍然做错了什么。 $(document).ready(function(){ $('.arrow').click(function
我有一个自定义对话框并使用它来代替 optionMenu。所以我希望 myDialog 表现得像菜单,即在按下菜单时显示/隐藏。我尝试了很多变体,但结果相同: 因为我为 myDialog 设置了一个
在我的项目中,我通过 ViewPager 创建我的 tabBar,如下所示: MainActivity.java mViewPager = (ViewPager) findViewById(R.id.
我目前正在使用一个 Excel 表,我将第 1-17 行分组并在单元格 B18 中写入了一个单元格值。我想知道当我在展开/折叠行时单击 +/- 符号时是否有办法更改 B18 中的值。 例如:我希望 B
我想创建一个按钮来使用 VBA 隐藏和取消隐藏特定组。我拥有的代码将隐藏或取消隐藏指定级别中的所有组: Sub Macro1() ActiveSheet.Outline.ShowLevels RowL
我是 VBA 新手。我想隐藏从任何行到工作表末尾的所有行。 我遇到的问题是我不知道如何编程以隐藏最后写入的行。 我使用下一个函数知道最后写入的单元格,但我不知道在哪里放置隐藏函数。 last = Ra
我想根据另一个字段的条件在 UI 上隐藏或更新一个字段。 例如,如果我有一个名为 Color 的字段: [PXUIField(DisplayName="Color")] [PXStringList("
这是我尝试开始收集通常不会遇到的 GCC 特殊功能。这是@jlebedev 在另一个问题中提到g++的“有效C++”选项之后, -Weffc++ This option warns about C++
我开发了一个 Flutter 应用程序,我使用了 ProgressDialog小部件 ( progress_dialog: ^1.2.0 )。首先,我展示了 ProgressDialog小部件和一些代
我需要在 API 17+ 的同一个 Activity(Fragment) 中显示/隐藏状态栏。假设一个按钮将隐藏它,另一个按钮将显示它: 节目: getActivity().getWindow().s
是否可以通过组件的 ts 代码以编程方式控制下拉列表的显示/隐藏(使用 Angular2 清楚)- https://vmware.github.io/clarity/documentation/dro
我想根据 if 函数的结果隐藏/显示 NiceScroll。 在我的html中有三个部分,从左到右逐一滚动。 我的脚本如下: var section2 = $('#section2').offset(
我有这个 jquery 代码: $(document).ready(function(){ //global vars var searchBoxes = $(".box"); var searchB
这个问题已经有答案了: Does something like jQuery.toggle(boolean) exist? (5 个回答) 已关闭 6 年前。 在 jQuery 中(我当前使用的是 1
我在这样的选择标签上使用 jQuery 的 selectMenu。 $('#ddlReport').selectmenu() 在某些情况下我想隐藏它,但我不知道如何隐藏。 这不起作用: $('#ddl
我是一名优秀的程序员,十分优秀!