我的 Vue 实例有两种方法;
const app = new Vue({
el: '#app',
data: {
id: null
},
methods: {
getId: function() {
return axios.get('url')
.then(response => response.data)
.then(id => this.id = id)
.catch(error => console.log(error))
},
foo: function() {
console.log(this.id)
}
},
mounted: function() {
this.getId()
this.foo()
}
})
Run Code Online (Sandbox Code Playgroud)
将console.log()日志null作为值,因为它在响应getId()设法设置id值之前运行。我知道这一点是因为当我使用 Vue 开发人员工具时, id 是我期望的实际值而不是null.
如何确保getId()在运行前设置了值this.foo()?
我正在使用内置的WP REST API来创建帖子(自定义帖子类型).我已经完成了这部分工作,下面是我提交的一些JSON示例,用于创建新帖子.
{
"title": "The story of Dr Foo",
"content": "This is the story content",
"status":"publish",
"excerpt":"Dr Foo story"
}
Run Code Online (Sandbox Code Playgroud)
问题是,我如何传递分配的分类?我似乎无法找到任何人询问或如何做的痕迹?
对于上下文,server 1正在创建帖子和WP存在server 2.
我还尝试使用请求传递长元数据,然后我可以在WP服务器上循环并使用wp_set_post_terms()相应地设置分类.
为此,有一个(几乎)rest_insert _ {$ this-> post_type}在通过REST API创建或更新单个帖子后触发.
此方法的问题在于,在action_rest_insert_post_type()函数完成之后才会设置元数据.这意味着当我尝试检索当前的帖子元数据时,它不存在.
function action_rest_insert_post_type( $post, $request, $true ) {
$items = get_post_meta( $post->ID); // at this point in time, returns empty array.
//wp_set_terms()
}
Run Code Online (Sandbox Code Playgroud)
我已经确认它$post->ID正在按预期工作,只是元数据没有完全设置...
请指教.
我有两个表,一个用于存储lists,另一个用于存储history创建lists的表。这些lists都是非常临时的,可以通过多种方法删除它们,因此我reason在历史记录中添加了一个字段。
//Lists table
Schema::create('lists', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('message');
$table->uuid('uuid');
$table->timestamps();
});
//History table
Schema::create('history', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('message');
$table->string('reason');
$table->uuid('uuid');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
现在两者都有一个uuid字段,我可以生成一个实际的字符串来使用 Laravel 的辅助函数进行存储$uuid = (string) Str::uuid();
$list = new List;
$list->name = 'A basic fact of life';
$list->message = 'Pineapple does not belong on pizza.'
$uuid = (string) Str::uuid();
$list->uuid = $uuid;
$list->save();
Run Code Online (Sandbox Code Playgroud)
现在,当我成功地从 中删除一条记录时Lists,我还会在历史记录中创建一条新记录及其数据。
$list = …Run Code Online (Sandbox Code Playgroud) Laravel 5.2 应用程序。
我的控制器中有一个函数,它使用 order by 子句查询我的数据库,然后我对其进行分页。
public function foo() {
$results = DB::orderBy('name', 'desc')->paginate(15);
return view('index', ['results' => $results]);
}
Run Code Online (Sandbox Code Playgroud)
索引.blade.php
@foreach ($results as $result)
<tr>
<td>{{ //current $result position is what I need }}</td>
<td>{{ $result->name }}<td>
//etc...
</tr>
@endforeach
Run Code Online (Sandbox Code Playgroud)
如何输出当前的行顺序迭代?
例如
Rank | name | //page 1
1 bob
2 john
3 katie
Rank | name | //page 2
4 dave
5 michael
6 ruben
Run Code Online (Sandbox Code Playgroud)
通常我只会输出,id但由于我有一个 orderBy 子句,因此 ID 不会正确反映当前排名。如果我执行类似for()循环的操作,那么当我转到下一页时,它会将第二页上的第一个结果重置为排名 1,即使它是下一页。
Laravel 有实现这一目标的具体方法吗?我浏览了文档,但找不到我要找的东西。
我正在尝试找到一种方法从刀片文件中的类文件中调用方法.foo()使用$itemforeach循环中的变量.由于我在刀片文件中循环,我无法或者更确切地说,在视图中调用控制器方法是不好的做法,或者我听说过.
myController的
public function getData() {
$data = DB::paginate(10);
return view('view', ['data' => $data]);
}
public function foo($var) {
//do something with $var
return $var
}
Run Code Online (Sandbox Code Playgroud)
view.blade.php
@foreach ($data as $item)
<td>{{$item->key}}</td>
<td>{{ //myController::foo($item) is Essentially the output I need }} </td>
@endforeach
Run Code Online (Sandbox Code Playgroud)
由于$item是在foreach(在视图内部)生成的,所以我不知道如何在返回语句之前调用方法.
有什么建议?
假设您有这个简单的标记。
<div class="container">
<div class="row">
<div class="col-md-12">
<h1> title </h1>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如果我想添加一个margin-top将其h1向下移向容器的中心(默认情况下它会将其自身定位在最顶部),那么将其应用到h1,.row或 上会更好.col-md-12吗?
例如:
.row {
margin-top: 100px;
}
Run Code Online (Sandbox Code Playgroud)
更新:这不是一个“主要基于意见”的问题,因为根据我应用样式的位置,当涉及到事物的响应方面时,它可能会破坏预期的行为。达乌德·阿万(Dawood Awan)的回答是这个问题合理性的完美例子,并且它与观点无关。
编辑:你误解了。考虑这个伪代码。这基本上是我想要做的,但是当这样写时它不起作用。
使用 Fetch 收到 Laravel 422 响应后,将response不包含实际的 JSON 数据。您必须使用response => response.json()才能访问它。我已经放了一张照片,response我还提供了使用后的输出response => response.json()。
我不想将对象转换为 JSON 字符串。
我在 Laravel 中使用 SweetAlert2。
throw error(response => response.json())
swal({
title: 'Are you sure you want to add this resource?',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
allowOutsideClick: () => !swal.isLoading(),
preConfirm: () => {
return fetch("/resource/add", {
method: "POST",
body: JSON.stringify(data),
credentials: "same-origin",
headers: new Headers({
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
'Content-Type': 'application/json',
'Accept': 'application/json' …Run Code Online (Sandbox Code Playgroud) 我似乎遇到了Bootstrap旋转木马的问题.我最后一次实现一个是当我使用Bootstrap 2.xx时.从2.xx示例代码页中翻录工作正常.但是,当我这样做时,旋转木马似乎不起作用.不滑,没有.我已经包含了Bootstrap 3 CDN和路径工作.我还在页面底部包含了Jquery,这是他们用代码提供的那个,它仍然是一个实时链接.任何解决方案/建议都非常感谢.
谢谢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Veyern Gaming</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">>
<link rel="stylesheet" href="css/style.css"/>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!--Navbar-->
<div class="navbar">
<ul class="nav navbar-nav">
<li class="active"><a href="/index.html>Home</a></li>
<li><a href="">Forum</a></li>
<li><a href="">Vote</a></li>
<li><a href="">Store</a></li>
<li><a href="">Stats</a></li>
</ul>
</div>
<!--Image Slider-->
<div id="carousel-example-generic" class="carousel slide">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" …Run Code Online (Sandbox Code Playgroud) 所以我最近意识到,而不是使用
$users = User::all();
return view('home')->with('users', $users);
Run Code Online (Sandbox Code Playgroud)
你有能力做到
return view('home')->withUsers($users);
Run Code Online (Sandbox Code Playgroud)
我相信这些被称为“魔法方法”——这些有没有记录在某处?有我能找到的列表还是唯一的列表?