当前位置:   article > 正文

uniapp onLoad 注意事项_uniapp onload

uniapp onload

onLoad()页面加载执行,因为函数是从上到下执行的,所以调用方法和获取页面传递过来的值时,顺序要注意一下

<script>
	export default {
		data() {
			return {
				id:0,
				detail:{}
			}
		},
		methods: {
		async getNewsDetail(){
			console.log('yyyy'+this.id);
			const res = await this.$myRuquest({
					url:'/api/getnew/'+this.id,
					
				})
				console.log(res)
			this.detail = res.data.message[0]
			}
		},
		onLoad(options){
			
			this.getNewsDetail()
			this.id = options.id
		    // console.log(options.id);
		}
	}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

如上图,代码没错,但是先调用的函数,所以当函数执行时id的值还是0,并没有将id赋值成功,所以参数是空

正确写法

<script>
	export default {
		data() {
			return {
				id:0,
				detail:{}
			}
		},
		methods: {
		async getNewsDetail(){
			console.log('yyyy'+this.id);
			const res = await this.$myRuquest({
					url:'/api/getnew/'+this.id,
					
				})
				console.log(res)
			this.detail = res.data.message[0]
			}
		},
		onLoad(options){
			this.id = options.id
			this.getNewsDetail()
		
		    // console.log(options.id);
		}
	}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/284274
推荐阅读