函數式編程
2023-07-14 15:34:49
我正在嘗試運行這個 Reddit 克隆。我導入了不同類型的Vue CDN,<script src="https://unpkg.com/vue@next"></script>Uncaught TypeError: Vue.component is not a function"我不確定我是否導入了錯誤版本的 Vue,或者我在 jsfiddle 上的設置是否錯誤。var subreddit = Vue.component('subreddit',{template: '#subreddit',props: ['name'],data: function () { return { posts: [] }},created: function(){ this.$http.get("https://www.reddit.com/r/"+ this.name +"/top.json?limit=3") .then(function(resp){ this.posts=resp.data.data.children; });}});完整代碼在這里:https://jsfiddle.net/jm0vs2kn/22/
1 回答

蝴蝶不菲
TA貢獻1810條經驗 獲得超4個贊
您的腳本指向 Vue 版本 3?@next
,該版本在全局 API 中進行了一些重大更改,要與 Vue 2 配合使用,
?<script?src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
您還應該在文檔中創建一個 div,body
然后<body><div id="app"></div> ...
像這樣安裝 vue 應用程序new Vue({el: '#app'});
您的x-templates應在div
id之外定義app
,如下所示:
?<script type="text/x-template" id="subreddit">
? ? ? ? <div class="subreddit">
? ? ? ? ? ? <h2>{{ name | uppercase }}</h2>
? ? ? ? ? ? <ul class="item-list">
? ? ? ? ? ? ? ? <li v-for="obj in posts">
? ? ? ? ? ? ? ? ? ? <post :item="obj"></post>
? ? ? ? ? ? ? ? </li>
? ? ? ? ? ? </ul>
? ? ? ? </div>
? ? </script>
<div id="#app">
應該<div id="app">
并且.then(function(resp){
應該是一個箭頭函數.then((resp)=>{
下面的示例用于axios
進行 ajax 調用。
添加回答
舉報
0/150
提交
取消