我有一个函数,它接受一些参数并呈现一个 SVG。我想根据传递给函数的名称动态导入该 svg。它看起来像这样:
import React from 'react';
export default async ({name, size = 16, color = '#000'}) => {
const Icon = await import(/* webpackMode: "eager" */ `./icons/${name}.svg`);
return <Icon width={size} height={size} fill={color} />;
};
Run Code Online (Sandbox Code Playgroud)
根据动态导入的webpack 文档和神奇的注释“eager”:
“不生成额外的块。所有模块都包含在当前块中,并且不会发出额外的网络请求。仍然返回一个 Promise,但已经解决了。与静态导入相比,模块在调用导入之前不会执行() 制成。”
这就是我的图标解决的问题:
> Module
default: "static/media/antenna.11b95602.svg"
__esModule: true
Symbol(Symbol.toStringTag): "Module"
Run Code Online (Sandbox Code Playgroud)
试图以我的函数试图给我这个错误的方式呈现它:
Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
我不明白如何使用这个导入的 Module …
对不起标题,我太沮丧了,现在想出更好的东西.
我有一个类,Judge它有一个方法#stats.该stats方法应该向api发送GET请求并获取一些数据作为响应.我正在尝试测试这个并存根stats方法,以便我不执行实际请求.这是我的测试看起来像:
describe Judge do
describe '.stats' do
context 'when success' do
subject { Judge.stats }
it 'returns stats' do
allow(Faraday).to receive(:get).and_return('some data')
expect(subject.status).to eq 200
expect(subject).to be_success
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是我正在测试的课程:
class Judge
def self.stats
Faraday.get "some-domain-dot-com/stats"
end
end
Run Code Online (Sandbox Code Playgroud)
这当前给了我错误:Faraday does not implement: get
那你如何用法拉第存根?我见过的方法如下:
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.get('http://stats-api.com') { [200, {}, 'Lorem ipsum'] }
end
Run Code Online (Sandbox Code Playgroud)
但我似乎无法以正确的方式应用它.我在这里错过了什么?
场景:我有一个Rails 4.0.0应用程序,使用capistrano部署,它在我的生产服务器上预编译我的资产.
问题:我正在尝试添加字体并将其与@ font-face一起使用.它在本地工作,但不在生产中.
错误消息: "无法加载资源:服务器响应状态为404(未找到)"
我的字体位于app/assets/fonts /
我的相关文件:
应用程序/资产/样式表/ application.css.scss:
/*
* This is a manifest file yada yada yada...
*
*= require bootstrap
*= require_self
*= require_tree .
*/
@font-face {
font-family: 'stone_sansregular';
src: url(font-path('stone_sans_regular-webfont.eot') + "?#iefix") format('embedded-opentype'),
url(font-path('stone_sans_regular-webfont.woff')) format('woff'),
url(font-path('stone_sans_regular-webfont.ttf')) format('truetype'),
url(font-path('stone_sans_regular-webfont.svg') + "#stone_sansregular") format('svg');
}
Run Code Online (Sandbox Code Playgroud)
配置/ application.rb中:
config.assets.paths << Rails.root.join("app", "assets", "fonts")
Run Code Online (Sandbox Code Playgroud)
我在几个SO帖子和其他来源中寻找答案,但我似乎无法做到正确.顺便说一句,我没有在Heroku上部署.我错过了什么?我感谢您的帮助.
编辑: 在生产中,我发现我认为它们应该是的字体:my-rails-app/current/public/@ assets/fonts
我有两个模型,Recipe和Tag,有has_and_belongs_to_many关系。对于这种关系,我有一个简单的连接表,RecipesTags。
食谱:
has_and_belongs_to_many :tags
Run Code Online (Sandbox Code Playgroud)
标签:
has_and_belongs_to_many :recipes
Run Code Online (Sandbox Code Playgroud)
现在,在创建新食谱时,用户可以以复选框的形式填写该食谱所属的类别,例如“肉”、“鱼”等。这些类别实际上只是数据库中的标签。
问题:食谱没有保存任何标签。
配方new和create控制器方法:
def new
@recipe = Recipe.new
@ingredients = Ingredient.all
@tags = Tag.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @recipe }
end
end
# POST /recipes
# POST /recipes.json
def create
@recipe = Recipe.new(params[:recipe])
if (params[:tags])
@recipe.tags << params[:tags]
end
respond_to do |format|
if @recipe.save
format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
format.json …Run Code Online (Sandbox Code Playgroud) 我会尽力解释一下:
我有一个实体 - 图像 - 看起来像这样:
public class Image : IEntity
{
public int ID
{
get;
set;
}
public string name
{
get;
set;
}
public virtual ICollection<Restaurant> Restaurant
{
get;
set;
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是Restaurant实体类中的相关属性:
[HiddenInput(DisplayValue = false)]
public Guid ImageID { get; set; }
public string ImageName { get; set; }
public virtual Image Image
{
get;
set;
}
public byte[] ImageData
{
get; set;
}
[HiddenInput(DisplayValue = false)]
public string ImageMimeType
{
get; set;
}
Run Code Online (Sandbox Code Playgroud)
在我的视图中,用户拍摄图像并将其上传以便为用户保存,我有一个简单的,带有enctype …
我正在使用 D3 在 React 中制作折线图,并且当将鼠标悬停在包含当前数据点的数据的图表内时,我尝试显示工具提示。图表渲染正确,但是当我将鼠标悬停在其中时,我收到错误node.getBoundingClientRect is not a function。
我正在关注这个示例,它没有使用 React,那么它是否与 React 处理事物的方式有关?
这就是我的组件的样子:
class LineChart extends Component {
wrapper = React.createRef();
componentDidMount() {
const {data} = this.props
let svg = d3.select("body svg.mySvg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
const parseTime = d3.timeParse("%Y")
const bisectDate = d3.bisector(function(d) { return d.year; }).left;
const x = d3.scaleTime().range([0, width]);
const y …Run Code Online (Sandbox Code Playgroud)