Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js Vue 3合成API…如何替换getElementById_Vue.js_Vuejs3_Vue Composition Api - Fatal编程技术网

Vue.js Vue 3合成API…如何替换getElementById

Vue.js Vue 3合成API…如何替换getElementById,vue.js,vuejs3,vue-composition-api,Vue.js,Vuejs3,Vue Composition Api,我在VUE3合成API中编写了以下内容 如果您查看“onMounted”,我将在窗口上附加一个事件侦听器,以将状态框保留在屏幕底部。我使用绝对位置和底部0 我想知道是否可以通过替换getElementById使这个更“Vue”?我试过裁判,但没用 有什么建议吗?或者我应该不去管它吗 谢谢 清除所有通知 {{status.text}} 从“vue”导入{defineComponent,ref,computed,watch,onMounted}; 从“./model/DataStore”导入{us

我在VUE3合成API中编写了以下内容

如果您查看“onMounted”,我将在窗口上附加一个事件侦听器,以将状态框保留在屏幕底部。我使用绝对位置和底部0

我想知道是否可以通过替换getElementById使这个更“Vue”?我试过裁判,但没用

有什么建议吗?或者我应该不去管它吗

谢谢


清除所有通知
{{status.text}}
从“vue”导入{defineComponent,ref,computed,watch,onMounted};
从“./model/DataStore”导入{useStore};
导出默认定义组件({
设置(){
const store=useStore();
const status=ref([]);
const statusMessage=computed(()=>store.state.statusMessage);
函数addStatus(newMessage){
statuses.value.push({
id:statuses.value.length+1,
文本:newMessage
})
}
监视(statusMessage,(newValue:string,oldValue:string)=>{
addStatus(statusMessage.value);
})
未安装(()=>{
window.addEventListener(“滚动”,函数(e){
设垂直位置=0;
垂直位置=页面偏移;
if(document.getElementById(“幻灯片”)){
document.getElementById('slide').style.bottom=-(垂直位置)+'px';
}
});
})
返回{
商店,
身份,
addStatus
};
},
方法:{
clearAllNotifications(){
this.status=[]
},
关闭状态(elm:任何){
const index=this.statuses.map((status)=>status.id.indexOf(elm.id);
此.状态.拼接(索引,1);
}
}
})
下面是幻灯片模态样式:

。幻灯片模式{
最大高度:200px;
宽度:500px;
背景色:#F2F2;
颜色:#505050;
填充:8px;
显示器:flex;
弯曲方向:立柱;
间隙:8px;
溢出x:隐藏;
位置:绝对位置;
底部:0;
}
这个例子非常简单


这是一个根元素
从“vue”导入{ref,onMounted}
导出默认值{
设置(){
const root=ref(空)
未安装(()=>{
//初始渲染后,DOM元素将被指定给ref
console.log(root.value)//这是一个根元素
})
返回{
根
}
}
}
  • 创建时使用ref()
  • 在返回函数中传递给模板
  • 在模板中使用
    ref=“…”
  • 在安装的
    onMounted
    中,通过
    ref.value
所以对于你的代码,它将是


清除所有通知
{{status.text}}
从“vue”导入{defineComponent,ref,computed,watch,onMounted};
从“./model/DataStore”导入{useStore};
导出默认定义组件({
设置(){
const store=useStore();
const status=ref([]);
常量slideRef=ref();
const statusMessage=computed(()=>store.state.statusMessage);
函数addStatus(newMessage){
statuses.value.push({
id:statuses.value.length+1,
文本:newMessage
})
}
监视(statusMessage,(newValue:string,oldValue:string)=>{
addStatus(statusMessage.value);
})
未安装(()=>{
window.addEventListener(“滚动”,函数(e){
设垂直位置=0;
垂直位置=页面偏移;
如果(参考值){
ref.value.style.bottom=-(垂直位置)+“px”;
}
});
})
返回{
商店,
身份,
addStatus,
滑梯
};
},
})

替换为ref时的代码是什么?如果(document.getElementById(“slide”){document.getElementById(“slide”).style.bottom=-(垂直位置)+“px”;}我不想使用document.getElementById。