前提・実現したいこと
Googleニュースからデータを取ってきて、ニュースの一覧画面を表示したい。laravel & vue.jsを使用しています。
具体的には、
・Googleニュースから取ってきたデータを配列に格納し、view(blade)に渡す。
・bladeテンプレート内のvueコンポーネントにその配列を渡して表示する。
を行おうとしています。
発生している問題
blade内で使用しているvueコンポーネントが正しく表示されません。具体的には、下記のようにvueコンポーネントが展開されず、そのまま表示されてしまいます(各属性の値はたまたま取れたニュースのデータです)。
<news-article-component :title="トレードに「正しさ」はいらない - みんなの仮想通貨" :link="https://cc.minkabu.jp/column/1283" :description="<a href="https://cc.minkabu.jp/column/1283" target="_blank">トレードに「正しさ」はいらない</a>&nbsp;&nbsp;<font color="#6f6f6f">みんなの仮想通貨</font><p>パパ山根仮想通貨トレード日記(162)FXトレード歴10年。空いてる時間は三男(1歳)の育児にすべて注ぎ込む「イクメントレーダー」。 自身が開発したトレードシス.</p>" :pub-date=" Fri, 09 Aug 2019 04:02:54 GMT"> </news-article-component>
該当のソースコード
SampleController.php
php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6 7class SampleController extends Controller 8{ 9 //関連ニュース表示画面 10 public function showNews() 11 { 12 function get_news($search_word) 13 { 14 $query = urlencode(mb_convert_encoding($search_word, "UTF-8","ASCII,JIS,UTF-8,EUC-JP,SJIS")); 15 $res = simplexml_load_file("https://news.google.com/rss/search?q=${query}&hl=ja&gl=JP&ceid=JP:ja"); 16 if($res->channel->item) { 17 $sample_news = []; 18 $count = 0; 19 foreach($res->channel->item as $item) { 20 $sample_news[$count]["title"] = (string)$item->title; 21 $sample_news[$count]["link"] = (string)$item->link; 22 $sample_news[$count]["pubDate"] = (string)$item->pubDate; 23 $sample_news[$count]["description"] = (string)$item->description; 24 $count++; 25 } 26 } 27 return $sample_news; 28 } 29 $articles = get_news("検索したいワード"); 30 return view('sample.news', compact('articles')); 31 } 32} 33
news.blade.php
php
1@extends('layouts.base') 2 3@section('content') 4<div class="u-container"> 5 <div class="u-content-wrap"> 6 <section class="p-news"> 7 <h2 class="p-news__title"> 8 News 9 </h2> 10 <ul class="p-news__list"> 11 @foreach ($articles as $article) 12 <li id="app" class="p-news__item"> 13 <news-article-component 14 :title="{{ $article['title'] }}" 15 :link="{{ $article['link'] }}" 16 :description="{{ $article['description'] }}" 17 :pub-date=" {{ $article['pubDate'] }}"> 18 </news-article-component> 19 </li> 20 @endforeach 21 </ul> 22 </section> 23 </div> 24</div> 25@endsection
なお、vueコンポーネントの属性を削除し、<news-article-component></news-article-component>
とすると、vueコンポーネントの内容が表示されるようになります。
NewsArticleComponent.vue
JavaScript
1<template> 2 <article class="c-article"> 3 <div class="c-article__text-wrap"> 4 <h3 class="c-article__title">{{ title }}</h3> 5 <p class="c-article__excerpt">{{ description }}</p> 6 <p class="c-article__date">{{ pubDate }}</p> 7 </div> 8 <div class="c-article__img-wrap"> 9 <img src="../../img/coin_bg.jpg" alt="" class="c-article__img" /> 10 </div> 11 </article> 12</template> 13 14<script> 15export default { 16 props: { 17 title: { 18 type: String 19 }, 20 link: { 21 type: String 22 }, 23 description: { 24 type: String 25 }, 26 pubDate: { 27 type: String 28 } 29 } 30}; 31</script>
補足情報(FW/ツールのバージョンなど)
Laravel:5.8
vue:2.5.17
単純な所の書き間違いなどではないかと思うのですが、煮詰まってきたため、お力を貸して頂けると大変助かります。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/12 09:39
2019/08/12 10:34 編集