簡體   English   中英

帶有 Rails 的 Vue.js 無法正常工作

[英]Vue.js with Rails not working

我正在嘗試向 Rails 控制器發送 AJAX 調用以獲取一些數據並使用 Vue.js 輸出它。 但它似乎沒有向控制器發送任何類型的請求。 我在這里做錯了什么? Vue.js 可以在沒有 AJAX 調用的情況下工作

應用程序/資產/javascript/calculator.js

var calculator = new Vue({
  el: '.container',
  data: {
    numbers: []
  },
  ready: function() {
    var that;
    that = this;
    $.ajax({
      url: '/calculator.json',
      success: function(response) {
        that.numbers = response;
      }
    });
  }
});

應用程序/控制器/calculator_controller.rb

class CalculatorController < ApplicationController
  def index
    @numbers = [1,2,3,4,5]
    respond_to do |format|
      format.html
      format.json { render json: @numbers }
    end
  end
end

應用程序/視圖/計算器/index.html.haml

.container
  .row
    .col-lg-12
      %ul
        %li{ "v-for": "number in numbers" }
          {{ number }}

而不是ready嘗試mounted

var calculator = new Vue({
  el: '.container',
  data: {
    numbers: []
  },
  mounted: function() {
    var that;
    that = this;
    $.ajax({
      url: '/calculator.json',
      success: function(response) {
        that.numbers = response;
      }
    });
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM