1. 程式人生 > >vue.js中報錯: TypeError: Cannot read property 'indexOf' of undefined

vue.js中報錯: TypeError: Cannot read property 'indexOf' of undefined

[Vue warn]: Error when evaluating expression "name.indexOf('jack') >= 0": TypeError: Cannot read property 'indexOf' of undefined

ypeError: Cannot read property 'indexOf' of undefined,從這句話中indexOf()是一個函式,但是卻不能識別該屬性,說明引用物件出錯。

程式碼如下所示:

<!doctype html>
<!DOCTYPE html>
<html>
<head>
	<title>dd</title>
</head>
<body>
	<!-- view -->
	<div id="app">
		<h1 v-if="yes">yes</h1>
		<h1 v-if="no">no</h1>
		<h1 v-if="age>=25">Age:{{age}}</h1>
		<h1 v-if="name.indexOf('jack') >= 0"
>Name:{{name}}</h1> </div> </body> <script type="text/javascript" src="vue.js"></script> <script type="text/javascript"> // viewModel var vm = new Vue({ el:"#app", data:{ yes:true, no:false, age:27, naem:"jack" } }) </script> </html>
從上面程式碼看出,data的屬性“naem”和view中的“name”不一樣

總結:ypeError: Cannot read property 'indexOf' of undefined,一般情況是引用物件屬性出錯