[英]Alpine JS - VM11705:16 Uncaught TypeError: Cannot read properties of undefined (reading 'title') Error Issue
I made a widget using alpine js however, getting an error like and finding solution to my research.然而,我使用 alpine js 制作了一个小部件,遇到了类似的错误并为我的研究找到了解决方案。 What do you think is the error in my code?
您认为我的代码中的错误是什么? When I pull the data, the header part is said to be incorrect.
当我拉取数据时,据说header部分不正确。 But I can't see a problem.
但我看不出有什么问题。 Or I couldn't figure it out.
或者我想不通。
[Errors i see in console][1] [1]: https://i.stack.imgur.com/ofmW1.png [我在控制台中看到的错误][1] [1]:https://i.stack.imgur.com/ofmW1.png
<div class="text-base w-56 px-5 rounded-full overflow-hidden" x-data="{
textArray: [],
text: '',
textIndex: 0,
charIndex: 0,
pauseEnd: 2750,
cursorSpeed: 420,
pauseStart: 20,
typeSpeed: 50,
direction: 'forward'
}" x-init="(() => {
fetch('/wp-json/wp/v2/pages/219/?_fields=acf.positions&acf_format=standard')
.then(response => response.json())
.then(data => textArray = data.acf.positions );
let typingInterval = setInterval(startTyping, $data.typeSpeed);
function startTyping(){
let current = $data.textArray[$data.textIndex].title;
if($data.charIndex > current.length){
$data.direction = 'backward';
clearInterval(typingInterval);
setTimeout(function(){
typingInterval = setInterval(startTyping, $data.typeSpeed);
}, $data.pauseEnd);
}
$data.text = current.substring(0, $data.charIndex);
if($data.direction == 'forward'){
$data.charIndex += 1;
} else {
if($data.charIndex == 0){
$data.direction = 'forward';
clearInterval(typingInterval);
setTimeout(function(){
$data.textIndex += 1;
if($data.textIndex >= $data.textArray.length){
$data.textIndex = 0;
}
typingInterval = setInterval(startTyping, $data.typeSpeed);
}, $data.pauseStart);
}
$data.charIndex -= 1;
}
}
setInterval(function(){
if($refs.cursor.classList.contains('hidden')){
$refs.cursor.classList.remove('hidden');
} else {
$refs.cursor.classList.add('hidden');
}
}, $data.cursorSpeed);
})()">
<span x-text="text"></span>
<span class="opacity-70" x-ref="cursor">|</span>
</div>
Thanks in advance for your suggestions and help.提前感谢您的建议和帮助。
It's complaining that $data.textArray[$data.textIndex]
is undefined, and you're trying to read .title
from it.它抱怨
$data.textArray[$data.textIndex]
未定义,而您正试图从中读取.title
。
when you first load your widget you do a fetch to populate textArray
, but until that returns your textArray
is empty so when you're trying to call let current = $data.textArray[$data.textIndex].title
it's returning the error当你第一次加载你的小部件时,你会做一个 fetch 来填充
textArray
,但是在返回之前你的textArray
是空的,所以当你试图调用let current = $data.textArray[$data.textIndex].title
时它会返回错误
You basically need to ensure textArray has data before you try to access it:在尝试访问之前,您基本上需要确保 textArray 有数据:
.then(data => textArray = data.acf.positions );
.then(data => textArray = data.acf.positions );
so it's only started when the fetch has been called.if (textArray.length === 0) return;
if (textArray.length === 0) return;
line at the start of your startTyping
function so it doesn't try to do anything until textArray is populated startTyping
function 开头的行,因此在填充 textArray 之前它不会尝试做任何事情might not be your only issue, but this is the cause of the error you've posted可能不是您唯一的问题,但这是您发布的错误的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.